夜深了、废话不多说了,项目需要改变Android软键盘右角下的功能键!
好了!先看图?还是代码?.... 还是先代码、然后效果图!
代码:
public class MainActivity extends Activity {
EditText editText1, editText2, editText3, editText4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
property();
}
/**
* 获取ID 事件
*/
private void init() {
editText1 = (EditText) findViewById(R.id.editText1);
editText2 = (EditText) findViewById(R.id.editText2);
editText3 = (EditText) findViewById(R.id.editText3);
editText4 = (EditText) findViewById(R.id.editText4);
editText1.setOnEditorActionListener(new getOnEditorActionListener());
editText2.setOnEditorActionListener(new getOnEditorActionListener());
editText3.setOnEditorActionListener(new getOnEditorActionListener());
editText4.setOnEditorActionListener(new getOnEditorActionListener());
}
/**
* 赋值样式(这里是java代码给、也可以xml布局给)
*/
private void property() {
// 搜素
editText1.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
editText1.setInputType(EditorInfo.TYPE_CLASS_TEXT);
// 下一个
editText2.setImeOptions(EditorInfo.IME_ACTION_NEXT);
editText2.setInputType(EditorInfo.TYPE_CLASS_TEXT);
// 发送
editText3.setImeOptions(EditorInfo.IME_ACTION_SEND);
editText3.setInputType(EditorInfo.TYPE_CLASS_TEXT);
// 完成
editText4.setImeOptions(EditorInfo.IME_ACTION_DONE);
editText4.setInputType(EditorInfo.TYPE_CLASS_TEXT);
}
/**
* 内部内 实现了 OnEditorActionListener接口
* @author asus
*
*/
class getOnEditorActionListener implements OnEditorActionListener {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
switch (actionId) {
//当然这里判断是用标识列 int(相匹配)
//假如我把 “搜素”换成 int(3) 也是成立
case EditorInfo.IME_ACTION_SEARCH:
Toast.makeText(getApplicationContext(), "你点击了 搜素! 内容为:"+v.getText(), 3).show();
break;
case EditorInfo.IME_ACTION_NEXT:
Toast.makeText(getApplicationContext(), "你点击了 下一个! 内容为:"+v.getText(), 3).show();
break;
case EditorInfo.IME_ACTION_SEND:
Toast.makeText(getApplicationContext(), "你点击了 发送! 内容为:"+v.getText(), 3).show();
break;
case EditorInfo.IME_ACTION_DONE:
Toast.makeText(getApplicationContext(), "你点击了 完成! 内容为:"+v.getText(), 3).show();
break;
default:
break;
}
return false;
}
}
主要代码:
public abstract boolean onEditorAction (TextView v, int actionId, KeyEvent event){}
参数
v | 执行动作的视图 |
---|---|
actionId | 动作标识. 该值可以是当回车键按下,调用该函数时你指定的值, 或者 EditorInfo.IME_NULL . |
event | 如果由回车键触发,这是其事件;否则为空. |
布局不贴、稍后提供源码下载!
效果图:
1.搜素
2.下一个
3.发送
4.确定
******************************************************
(扩展)
1.Android软键盘 显示
一般情况只有获得焦点,然后在调用系统软键盘
获得焦点:
editText1.requestFocus();
弹出软键盘:(开启一个定时器,弹出软键盘,前提要获得焦点!)
// 设置定时器(A跳到B页面,在B页面定时出现键盘,当然如果你是单一事件就不用、前提一定要获得焦点)
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
// 弹出软键盘的代码
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText1, InputMethodManager.RESULT_SHOWN);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,
InputMethodManager.HIDE_IMPLICIT_ONLY);
}
}, 10);//定义出现时间值
2.Android软键盘 关闭
// 关闭软键盘
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText1.getWindowToken(), 0);
以上源代码:点击打开链接
转载请附出处:http://blog.csdn.net/cs_li1126/article/details/12630433
关于EditText属性 可以参考:
①:点击打开链接
②:点击打开链接
附:
软键盘的Enter键默认显示的是“完成”文本,通过设置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