Android 中的 EditText onClickListener
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2359176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
EditText onClickListener in Android
提问by xger86x
I want an EditText which creates a DatePicker when is pressed. So I write the following code:
我想要一个 EditText,它在按下时创建一个 DatePicker。所以我写了下面的代码:
mEditInit = (EditText) findViewById(R.id.date_init);
mEditInit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DATEINIT_DIALOG);
}
});
But when I press the EditText the action is the typical: a cursor waiting for typing text instead show the Dialog I want.
但是当我按下 EditText 时,动作是典型的:等待输入文本的光标显示我想要的对话框。
回答by Dillon Kearns
The keyboard seems to pop up when the EditText gains focus. To prevent this, set focusable to false:
当 EditText 获得焦点时,键盘似乎弹出。为了防止这种情况,将focusable设置为false:
<EditText
...
android:focusable="false"
... />
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:
这种行为可能因不同制造商的 Android 操作系统风格而异,但在我测试过的设备上,我发现这已经足够了。如果键盘仍然弹出,使用提示而不是文本似乎也有帮助:
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() {...});
回答by caw
Normally, you want maximum compatibility with EditText
's normal behaviour.
通常,您希望与EditText
的正常行为最大程度地兼容。
So you should notuse android:focusable="false"
as, yes, the view will just not be focusable anymore which looks bad. The background drawable will not show its "pressed" state anymore, for example.
所以,你应该不使用android:focusable="false"
的,是的,视图将只是不可聚焦了它的日子也不好过。例如,背景可绘制对象将不再显示其“按下”状态。
What you should do instead is the following:
你应该做的是:
myEditText.setInputType(InputType.TYPE_NULL);
myEditText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// showMyDialog();
}
});
myEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
// showMyDialog();
}
}
});
By setting the input type to TYPE_NULL
, you prevent the software keyboard from popping up.
通过将输入类型设置为TYPE_NULL
,可以防止软件键盘弹出。
By setting the OnClickListener
and OnFocusChangeListener
, you make sure that your dialog will always open when the user clicks into the EditText
field, both when it gains focus (first click) and on subsequent clicks.
通过设置OnClickListener
and OnFocusChangeListener
,您可以确保您的对话框在用户单击该EditText
字段时始终打开,无论是在获得焦点(第一次单击)还是在后续单击时。
Just setting android:inputType="none"
or setInputType(InputType.TYPE_NULL)
is not always enough. For some devices, you should set android:editable="false"
in XML as well, although it is deprecated. If it does not work anymore, it will just be ignored (as all XML attributes that are not supported).
仅仅设置android:inputType="none"
或setInputType(InputType.TYPE_NULL)
并不总是足够的。对于某些设备,您也应该android:editable="false"
在 XML 中进行设置,尽管它已被弃用。如果它不再起作用,它将被忽略(因为所有不支持的 XML 属性)。
回答by Yusuf Hoque
I had this same problem. The code is fine but make sure you change the focusable value of the EditText to false.
我有同样的问题。代码很好,但请确保将 EditText 的可聚焦值更改为 false。
<EditText
android:id="@+id/date"
android:focusable="false"/>
I hope this helps anyone who has had a similar problem!
我希望这可以帮助任何遇到类似问题的人!
回答by Akshay Chordiya
Default working of EditText
:
On first click it focuses and on second click it handles onClickListener
so you need to disable focus. Then on first click the onClickListener
will handle.
默认工作方式EditText
:第一次单击时聚焦,第二次单击时处理,onClickListener
因此您需要禁用焦点。然后在第一次单击onClickListener
将处理。
To do that you need to add this android:focusableInTouchMode="false"
attribute to your EditText
. That's it!
为此,您需要将此android:focusableInTouchMode="false"
属性添加到您的EditText
. 就是这样!
Something like this:
像这样的东西:
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusableInTouchMode="false"
android:inputType="text" />
回答by Raymond Chenon
Here is the solution I implemented
这是我实施的解决方案
mPickDate.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
showDialog(DATE_DIALOG_ID);
return false;
}
});
OR
或者
mPickDate.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
showDialog(DATE_DIALOG_ID);
}
});
See the differences by yourself. Problem is since (like RickNotFred said) TextView to display the date & edit via the DatePicker. TextEdit is not used for its primary purpose. If you want the DatePicker to re-pop up, you need to input delete (1st case) or de focus (2nd case).
请自行查看差异。问题是因为(如 RickNotFred 所说)TextView 通过 DatePicker 显示日期和编辑。TextEdit 不用于其主要目的。如果要重新弹出DatePicker,则需要输入delete(第一种情况)或de focus(第二种情况)。
Ray
射线
回答by fishjd
IMHO I disagree with RickNotFred's statement:
恕我直言,我不同意 RickNotFred 的声明:
Popping a dialog when an EditText gets focus seems like a non-standard interface.
当 EditText 获得焦点时弹出一个对话框似乎是一个非标准的界面。
Displaying a dialog to edit the date when the use presses the an EditText is very similar to the default, which is to display a keyboard ora numeric key pad. The fact that the date is displayed with the EditText signals to the user that the date may be changed. Displaying the date as a non-editable TextView signals to the user that the date may not be changed.
当用户按下 EditText 时显示一个对话框来编辑日期与默认情况非常相似,即显示一个键盘或一个数字小键盘。日期与 EditText 一起显示的事实向用户表明日期可能会更改。将日期显示为不可编辑的 TextView 向用户表明日期不可更改。
回答by V.March
If you use OnClick action on EditText like:
如果您在 EditText 上使用 OnClick 操作,例如:
java:
爪哇:
mEditInit = (EditText) findViewById(R.id.date_init);
mEditInit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DATEINIT_DIALOG);
}
});
or kotlin:
或科特林:
editTextChooseDate.setOnClickListener {
showDialog(DATEINIT_DIALOG)
}
So, it will work perfectly if you put into xml
of your EditText
the following lines:
因此,如果xml
您EditText
输入以下几行,它将完美运行:
android:inputType="none"
android:focusable="false"
android:cursorVisible="false"
android:inputType="none
"
android:focusable="false" android:cursorVisible="false"
For example:
例如:
<EditText
android:id="@+id/date_init"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:hint="Select Date"
android:inputType="none"
android:focusable="false"
android:cursorVisible="false"/>
or for MaterialDesign
或 MaterialDesign
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/layoutEditTextChooseDate"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/date_init"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:hint="Select Date"
android:inputType="none"
android:focusable="false"
android:cursorVisible="false"/>
</com.google.android.material.textfield.TextInputLayout>
回答by CoolMind
Nice topic. Well, I have done so. In XML file:
不错的话题。嗯,我已经这样做了。在 XML 文件中:
<EditText
...
android:editable="false"
android:inputType="none" />
In Java-code:
在 Java 代码中:
txtDay.setOnClickListener(onOnClickEvent);
txtDay.setOnFocusChangeListener(onFocusChangeEvent);
private View.OnClickListener onOnClickEvent = new View.OnClickListener() {
@Override
public void onClick(View view) {
dpDialog.show();
}
};
private View.OnFocusChangeListener onFocusChangeEvent = new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus)
dpDialog.show();
}
};
回答by Lukas Batteau
The following works perfectly for me.
以下对我来说非常有效。
First set your date picker widget's input to 'none' to prevent the soft keyboard from popping up:
首先将日期选择器小部件的输入设置为“无”以防止弹出软键盘:
<EditText android:inputType="none" ... ></EditText>
Then add these event listeners to show the dialog containing the date picker:
然后添加这些事件侦听器以显示包含日期选择器的对话框:
// Date picker
EditText dateEdit = (EditText) findViewById(R.id.date);
dateOfBirthEdit.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
showDialog(DIALOG_DATE_PICKER);
}
return false;
}
});
dateEdit.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
showDialog(DIALOG_DATE_PICKER);
} else {
dismissDialog(DIALOG_DATE_PICKER);
}
}
});
One last thing. To make sure typed days, months, or years are correctly copied from the date picker, call datePicker.clearFocus()
before retrieving the values, for instance via getMonth()
.
最后一件事。要确保从日期选择器中正确复制键入的天、月或年,请datePicker.clearFocus()
在检索值之前调用,例如通过getMonth()
.
回答by Rahim Rahimov
This Works For me:
这对我有用:
mEditInit = (EditText) findViewById(R.id.date_init);
mEditInit.setKeyListener(null);
mEditInit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus)
{
mEditInit.callOnClick();
}
}
});
mEditInit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DATEINIT_DIALOG);
}
});