<EditText android:id="@+id/mess" android:layout_width="wrap_content" android:layout_height="wrap_content" android:imeOptions="actionSend" android:inputType="text" />
mess = (EditText) findViewById(R.id.mess); mess.setOnEditorActionListener(new OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_SEND) { Toast.makeText(MessageActivity.this,"fasong",0).show(); } return false; } });
EditText通过设置android:imeOptions来改变默认的”文本或者样式。这里举几个常用的常量值:
actionUnspecified 未指定,对应常量EditorInfo.IME_ACTION_UNSPECIFIED.
actionNone 没有动作,对应常量EditorInfo.IME_ACTION_NONE
actionGo 去往,对应常量EditorInfo.IME_ACTION_GO
actionSearch 搜索,对应常量EditorInfo.IME_ACTION_SEARCH
actionSend 发送,对应常量EditorInfo.IME_ACTION_SEND
actionNext 下一个,对应常量EditorInfo.IME_ACTION_NEXT
actionDone 完成,对应常量EditorInfo.IME_ACTION_DONE
监听键盘的弹起和隐藏
package com.example.testjianpan; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnLayoutChangeListener; import android.widget.Button; import android.widget.EditText; import android.widget.RelativeLayout; import android.widget.Toast; public class MainActivity extends Activity implements OnLayoutChangeListener{ private EditText edt; private RelativeLayout rl; //屏幕高度 private int screenHeight = 0; //软件盘弹起后所占高度阀值 private int keyHeight = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); edt = (EditText) findViewById(R.id.edt); rl = (RelativeLayout) findViewById(R.id.rl); //获取屏幕高度 screenHeight = this.getWindowManager().getDefaultDisplay().getHeight(); //阀值设置为屏幕高度的1/3 keyHeight = screenHeight/3; Log.v("屏幕高度",screenHeight+""); rl.addOnLayoutChangeListener(this) ; } @Override public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { //现在认为只要控件将Activity向上推的高度超过了1/3屏幕高,就认为软键盘弹起 if(oldBottom != 0 && bottom != 0 &&(oldBottom - bottom > keyHeight)){ Toast.makeText(MainActivity.this, "监听到软键盘弹起...", Toast.LENGTH_SHORT).show(); }else if(oldBottom != 0 && bottom != 0 &&(bottom - oldBottom > keyHeight)){ Toast.makeText(MainActivity.this, "监听到软件盘关闭...", Toast.LENGTH_SHORT).show(); } } }