Android 项目中用到Edittext 点击时 根据不同属性 在输入法中显示不同的属性 并相应
Edittext在xml中添加相应的属性android:imeOptions
actionSearch 输入法右下角显示“搜索”
actionSend 输入法右下角显示“发送”
actionDone 输入法右下角显示“完成”
actionNext 输入法右下角显示“下一项”
actionPrevious 输入法右下角显示“上一项”
actionGo 输入法右下角显示“前往”
对应Action
EditorInfo.IME_ACTION_SEARCH
EditorInfo.IME_ACTION_SEND
EditorInfo.IME_ACTION_DONE
EditorInfo.IME_ACTION_NEXT
EditorInfo.IME_ACTION_PREVIOUS
EditorInfo.IME_ACTION_GO
监听
TextView.OnEditorActionListener
java代码
package com.patient.dchealth.edittextdemo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private EditText editText1;
private EditText editText2;
private EditText editText3;
private EditText editText4;
private EditText editText5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText1= (EditText) findViewById(R.id.activity_main_edittext1);
editText2= (EditText) findViewById(R.id.activity_main_edittext2);
editText3= (EditText) findViewById(R.id.activity_main_edittext3);
editText4= (EditText) findViewById(R.id.activity_main_edittext4);
editText5= (EditText) findViewById(R.id.activity_main_edittext5);
editText1.setOnEditorActionListener(new mEditorActionListener());
editText2.setOnEditorActionListener(new mEditorActionListener());
editText3.setOnEditorActionListener(new mEditorActionListener());
editText4.setOnEditorActionListener(new mEditorActionListener());
editText5.setOnEditorActionListener(new mEditorActionListener());
}
private class mEditorActionListener implements TextView.OnEditorActionListener {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
switch(v.getId()){
case R.id.activity_main_edittext1://搜索
if(actionId == EditorInfo.IME_ACTION_SEARCH) {
Toast.makeText(MainActivity.this,"搜索!",Toast.LENGTH_SHORT).show();
}
break;
case R.id.activity_main_edittext2://发送
if(actionId == EditorInfo.IME_ACTION_SEND) {
Toast.makeText(MainActivity.this,"发送!",Toast.LENGTH_SHORT).show();
}
break;
case R.id.activity_main_edittext3://完成
if(actionId == EditorInfo.IME_ACTION_DONE) {
Toast.makeText(MainActivity.this,"完成!",Toast.LENGTH_SHORT).show();
}
break;
case R.id.activity_main_edittext4://下一个
if(actionId == EditorInfo.IME_ACTION_NEXT) {
Toast.makeText(MainActivity.this,"下一个!",Toast.LENGTH_SHORT).show();
}
break;
case R.id.activity_main_edittext5://上一个
if(actionId == EditorInfo.IME_ACTION_PREVIOUS) {
Toast.makeText(MainActivity.this,"上一个!",Toast.LENGTH_SHORT).show();
}
break;
case R.id.activity_main_edittext6://前往
if(actionId == EditorInfo.IME_ACTION_GO) {
Toast.makeText(MainActivity.this,"前往!",Toast.LENGTH_SHORT).show();
}
break;
}
return false;
}
}
}
xml布局
<?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">
<EditText
android:id="@+id/activity_main_edittext1"
android:layout_width="match_parent"
android:layout_height="60dp"
android:hint="搜索"
android:imeOptions="actionSearch"
android:inputType="text"
android:singleLine="true"
android:textColor="#000000"
android:textColorHint="#000000" />
<EditText
android:id="@+id/activity_main_edittext2"
android:layout_width="match_parent"
android:layout_height="60dp"
android:hint="发送"
android:imeOptions="actionSend"
android:inputType="text"
android:singleLine="true"
android:textColor="#000000"
android:textColorHint="#000000" />
<EditText
android:id="@+id/activity_main_edittext3"
android:layout_width="match_parent"
android:layout_height="60dp"
android:hint="完成"
android:imeOptions="actionDone"
android:inputType="text"
android:singleLine="true"
android:textColor="#000000"
android:textColorHint="#000000" />
<EditText
android:id="@+id/activity_main_edittext4"
android:layout_width="match_parent"
android:layout_height="60dp"
android:hint="下一个"
android:imeOptions="actionNext"
android:inputType="text"
android:singleLine="true"
android:textColor="#000000"
android:textColorHint="#000000" />
<EditText
android:id="@+id/activity_main_edittext5"
android:layout_width="match_parent"
android:layout_height="60dp"
android:hint="上一个"
android:imeOptions="actionPrevious"
android:inputType="text"
android:singleLine="true"
android:textColor="#000000"
android:textColorHint="#000000" />
<EditText
android:id="@+id/activity_main_edittext6"
android:layout_width="match_parent"
android:layout_height="60dp"
android:hint="前往"
android:imeOptions="actionGo"
android:inputType="text"
android:singleLine="true"
android:textColor="#000000"
android:textColorHint="#000000" />
</LinearLayout>
效果