在做应用开发时,用户在用EditText进行输入时,我们可以充分利用软键盘特色功能进行导航,给用户提供好的用户体验。
举例说明android:imeOptions 属性特点
1 默认情况下软键盘右下角的按钮为“下一个”,点击会到下一个输入框,保持软键盘
2 android:imeOptions="actionSend"软键盘下方变成“发送”,点击后光标移动下一个
3 设置android:imeOptions="actionDone",软键盘下方变成“完成”,点击后光标保持在原来的输入框上,并且软键盘关闭
使用方法 给EditText 设置OnEditorActionListener,然后进行相应处理
附上Demo代码
public class IMFActivity extends Activity {
EditText etDefault;
EditText etEmail;
EditText etNumber;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.imf_layout);
etDefault = (EditText)findViewById(R.id.default_content);
etEmail = (EditText)findViewById(R.id.email_content);
etNumber = (EditText)findViewById(R.id.number_content);
etDefault.setOnEditorActionListener(onEditorActionListener);
etEmail.setOnEditorActionListener(onEditorActionListener);
etNumber.setOnEditorActionListener(onEditorActionListener);
}
private OnEditorActionListener onEditorActionListener = new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
switch(actionId){
case EditorInfo.IME_NULL:
System.out.println("null for default_content: " + v.getText() );
break;
case EditorInfo.IME_ACTION_SEND:
System.out.println("action send for email_content: " + v.getText());
break;
case EditorInfo.IME_ACTION_DONE:
System.out.println("action done for number_content: " + v.getText());
break;
}
return true;
}
};
}
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<TableLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1"
>
<TableRow>
<TextView android:text="username" android:id="@+id/TextView01"
android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<EditText android:text="" android:id="@+id/default_content"
android:layout_width="fill_parent" android:layout_height="wrap_content"></EditText>
</TableRow>
<TableRow>
<TextView android:text="email:" android:id="@+id/TextView01"
android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<EditText android:text="" android:id="@+id/email_content"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:inputType="text|textEmailAddress"
android:imeOptions="actionSend"></EditText>
</TableRow>
<TableRow>
<TextView android:text="password:" android:id="@+id/TextView01"
android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<EditText android:text="" android:id="@+id/number_content"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:inputType="number|numberSigned|numberDecimal"
android:imeOptions="actionDone"></EditText>
</TableRow>
</TableLayout>
</ScrollView>