Android 軟鍵盤按鍵監控

來源:互聯網
上載者:User

最近在做項目,遇到一個比較頭疼的問題,問題是需要對使用者的輸入進行時時監聽,而大部分使用者的輸入是通過軟鍵盤來完成的,而Android平台好象沒有專門的對此監控事件,那該怎麼辦呢?

最終解決辦法就是通過EditText和TextWatcher類來輔助監聽。具體做法如下:

private class TextMonitor implements TextWatcher{ @Overridepublic void beforeTextChanged(CharSequence s, int start, int count,int after) {//輸入前的內容String str_forward=s.toString().length();}@Overridepublic void onTextChanged(CharSequence s, int start, int before,int count) {//正在輸入的內容String str=s.toString().substring(start);tv.setText("您已經編輯的內容:"+s.toString());if(str.contains("\n")){//斷行符號鍵}}@Overridepublic void afterTextChanged(Editable s) {//輸入後的內容String str_last=s.toString().length();}   }

相應控制項:

private TextView tv;private EditText edit;tv=(TextView)findViewById(R.id.showInput);edit=(EditText)findViewById(R.id.InputContent);edit.addTextChangedListener(new TextMonitor());

布局檔案:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <TextView        android:id="@+id/showInput"       android:layout_width="fill_parent"         android:layout_height="wrap_content"        android:text="顯示您的編輯內容"        />       <EditText         android:id="@+id/InputContent"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:hint="輸入編輯內容"          /></LinearLayout>

目前好像還沒有其他的好辦法,只能這樣間接監控,歡迎大家能提出更好的解決辦法。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.