Android OnClick 事件仅在 edittext 上第二次起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12560519/
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
OnClick event only works second time on edittext
提问by Robby Smet
I have an edittext, and when the user clicks this edittext I want to show an alertdialog.
My code is the following :
我有一个编辑文本,当用户点击这个编辑文本时,我想显示一个警报对话框。
我的代码如下:
edt.setInputType(InputType.TYPE_NULL);
edt.setFocusableInTouchMode(true);
edt.requestFocus();
edt.setCursorVisible(false);
edt.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
CommentDialog.buildDialog(mContext, identifier, false, edt.getId());
}
});
I don't want the keyboard to show up when the user clicks the edittext, so I set the inputtype to TYPE_NULL.
But when the edittext doesn't have focus and I click it, the onClick event isn't executed. When I click it a second time, the alertdialog shows up correctly.
我不希望在用户单击编辑文本时显示键盘,因此我将输入类型设置为 TYPE_NULL。
但是当 edittext 没有焦点并且我单击它时,不会执行 onClick 事件。当我第二次单击它时,警报对话框正确显示。
How do I fix this?
我该如何解决?
回答by Simon Dorociak
Simply try to add this to your XML
file. Your keyboard pops up when widget
gains focus.
So to prevent this behaviour set focusable to false
. Then normal use OnClickListener
.
只需尝试将其添加到您的XML
文件中即可。widget
获得焦点时键盘会弹出。因此,为了防止这种行为将可聚焦设置为false
. 然后正常使用OnClickListener
。
<EditText
android:focusable="false"
...
/>
Now, it should works.
现在,它应该有效。
回答by Carnal
You can use onTouch
instead of onClick
, so it doesn't matter if the EditText
has focus or not.
您可以使用onTouch
代替onClick
,因此无论是否EditText
具有焦点都没有关系。
edt.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
CommentDialog.buildDialog(mContext, identifier, false, edt.getId());
return false;
}
});
回答by raghav chopra
Nothing much to do you just have to
没什么可做的,你只需要
edt.setFocusable(false);
回答by neaGaze
If focusableInTouchMode is true then touch is triggered in second touch only, so unless you want that case use false for focusableInTouchMode. and if you want to enable the focusability in the view set focusable true
如果 focusableInTouchMode 为 true,则仅在第二次触摸时触发触摸,因此除非您希望在这种情况下为 focusableInTouchMode 使用 false。如果您想在视图中启用可聚焦性,请设置 focusable true
<EditText android:focusable="true" android:focusableInTouchMode="false" ... />
回答by Ashwani
make your alert dialogbox appear on
使您的警报对话框出现在
setOnFocusChangedListener()
setOnFocusChangedListener()
回答by Andrei Lupsa
Avoid using a FocusChangeListener
since it will behave erratically when you don't really need it (eg. when you enter an activity). Just set an OnTouchListener
along with your OnClickListener
like this:
避免使用 a ,FocusChangeListener
因为当你真的不需要它时(例如,当你进入一个活动时)它会表现得不稳定。只需设置一个OnTouchListener
与你一起OnClickListener
这样的:
@Override
public boolean onTouch(View view, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
view.requestFocus();
break;
}
return false;
}
This will cause your EditText
to receive focus before your onClick call.
这将导致您EditText
在 onClick 调用之前获得焦点。
回答by Rahim Rahimov
You should add onFocusChangeListener:
您应该添加 onFocusChangeListener:
edt.setKeyListener(null);
edt.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus)
{
edt.callOnClick();
}
}
});
edt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CommentDialog.buildDialog(mContext, identifier, false, edt.getId());
}
});
回答by vishesh chandra
Instead of setting input type use "Editable=false" and "Focus=false" if you don't require keyboard.
如果不需要键盘,请使用“Editable=false”和“Focus=false”代替设置输入类型。
It maybe helpful to you.
也许对你有帮助。
回答by Joe Seibert
This was a real problem for me when trying to reproduce a "click" sound from the EditText when the soft keyboard pops up; I was only getting a click every second time. What fixed it for me was the the opposite of what worked for @neaGaze. This worked for me in my_layout.xml :
当软键盘弹出时,当我试图从 EditText 重现“点击”声音时,这对我来说是一个真正的问题;我每两次都只得到一次点击。对我来说修复它的东西与对@neaGaze 起作用的相反。这在 my_layout.xml 中对我有用:
<EditText android:focusable="true" android:focusableInTouchMode="true" ... />
It allows the click sound/event to happen each time when user enters the EditText, while also allowing the soft keyboard to show. You have to handle the OnClickListener of course for this to happen, even if you do nothing with it, like so :
它允许每次用户输入 EditText 时发生单击声音/事件,同时还允许显示软键盘。你当然必须处理 OnClickListener 才能发生这种情况,即使你什么都不做,就像这样:
myEditText = (EditText) findViewById(R.id.myEditText);
...
// implement the onClick listener so we get the click sound and event if needed
myEditText.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
//do something or nothing; up to you
}
});
Speaking of that pesky soft keyboard, if I finished from my Dialog style Activity with the soft keyboard up, no matter what I tried the keyboard remained up when I was returned to MainActivity. I had tried all the usual suggestions such as Close/Hide the Android soft keyboard, How to close Android soft keyboard programmaticallyetc. None of that worked.
说到那个讨厌的软键盘,如果我在软键盘打开的情况下完成了我的 Dialog 样式 Activity,无论我尝试什么,当我返回到 MainActivity 时,键盘都会保持打开状态。我已经尝试了所有常用的建议,例如关闭/隐藏 Android 软键盘、如何以编程方式关闭 Android 软键盘等。这些都没有奏效。
In my case I did not need the soft keyboard in MainActivity. What did work was the following in my AndroidManifest.xml file, within the MainActivity section
就我而言,我不需要 MainActivity 中的软键盘。在我的 AndroidManifest.xml 文件中的 MainActivity 部分中起作用的是以下内容
<activity
android:name=".MainActivity"
android:windowSoftInputMode="stateAlwaysHidden">
</activity>