Android中的EditText onClickListener [英] EditText onClickListener in Android
问题描述
我想要一个EditText,当按下它时会创建一个DatePicker.因此,我编写了以下代码:
I want an EditText which creates a DatePicker when is pressed. So I write the following code:
mEditInit = (EditText) findViewById(R.id.date_init);
mEditInit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DATEINIT_DIALOG);
}
});
但是当我按下EditText时,动作是典型的:光标在等待键入文本,而是显示我想要的对话框.
But when I press the EditText the action is the typical: a cursor waiting for typing text instead show the Dialog I want.
推荐答案
当EditText获得焦点时,键盘似乎弹出了.为防止这种情况,请将focusable设置为false:
The keyboard seems to pop up when the EditText gains focus. To prevent this, set focusable to false:
<EditText
...
android:focusable="false"
... />
此行为可能因不同制造商的Android OS风格而异,但对于 我测试过的设备已经足够了.如果键盘仍然弹出,则使用提示代替文本似乎也有帮助:
This behavior can vary on different manufacturers' Android OS flavors, but on the devices I've tested I have found this to to be sufficient. If the keyboard still pops up, using hints instead of text seems to help as well:
myEditText.setText("My text"); // instead of this...
myEditText.setHint("My text"); // try this
完成此操作后,您的点击监听器将按预期工作:
Once you've done this, your on click listener should work as desired:
myEditText.setOnClickListener(new OnClickListener() {...});
这篇关于Android中的EditText onClickListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!