我做了一个非常简单的测试应用程序的一个活动,一个布局。 该onClick
不会触发其压在第一时间,因为它应该。
活动:
package com.example.mytest;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText ed1 = (EditText) findViewById(R.id.editText1);
ed1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "1", Toast.LENGTH_LONG)
.show();
}
});
}
}
布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/editText1"
android:ems="10" />
</RelativeLayout>
如果你运行该应用程序,并单击第二editText
,然后再打开第一个,也不会触发onClick
。 您可以选择保持来回,它不会触发onClick
可言。 我需要这个基本功能,但一直没能想办法得到它的工作。 想法?
注意
我已经尝试了所有的建议对我的API级别16物理设备和我的API级别8模拟器的方法,但我得到了同样的问题。
澄清
当editText1
集中,并点击,那么onClick
方法火灾。 如果editText2
被聚焦,然后editText1
被点击,它不火。 大问题。
Answer 1:
概述,当用户与任何UI组件交互的各种听众被称为在自顶向下的顺序。 如果优先级较高的听众之一的“消费事件”,那么下听众会不会被调用 。
在你的情况这三个监听器调用顺序:
- OnTouchListener
- OnFocusChangeListener
- OnClickListener
第一次用户触摸它接收焦点,使得用户可以键入一个EditText。 该行动在这里消费。 为此优先级较低的OnClickListener不叫。 所以这些事件涓滴的OnClickListener每个连续的触摸不会更改焦点。
按钮(和其他类似成分)不从触摸事件接收焦点,这就是为什么OnClickListener被称为每次。
基本上,你有三种选择:
通过实施本身OnTouchListener:
ed1.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if(MotionEvent.ACTION_UP == event.getAction()) editTextClicked(); // Instead of your Toast return false; } });
这将执行每次的EditText被触摸的时间。 请注意收听返回false
,这使得事件逐步应用到内置OnFocusChangeListener这改变了焦点,使用户可以在输入的EditText。
实现与OnClickListener沿OnFocusChangeListener:
ed1.setOnFocusChangeListener(new OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { if(hasFocus) editTextClicked(); // Instead of your Toast } });
该监听捕获时,而你的OnClickListener捕获所有其他事件的焦点改变第一触摸事件。
(这不是一个有效的答案在这里,但它是一个不错的小技巧就知道了。)设置可聚焦属性false
在你的XML:
android:focusable="false"
现在OnClickListener就会触发每被点击一次。 但是这使得EditText上无用的,因为用户不再可以输入任何文字...
注意:
getApplicationContext()
可以创建内存泄漏。 一个好习惯是避免使用它,除非绝对必要的。 您可以放心地使用v.getContext()
来代替。
希望帮助!
Answer 2:
让编辑文字可点击。在XML android:clickable="true"
或代码
ed1.setClickable(true);
然后做
ed1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getBaseContext(), "1",Toast.LENGTH_SHORT).show();
}
});
Answer 3:
我可能来不及参加聚会,但这里是一个剪断的代码,修正了的onClick(问题)没有被调用:
ed1.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP && !v.hasFocus()) {
// onClick() is not called when the EditText doesn't have focus,
// onFocusChange() is called instead, which might have a different
// meaning. This condition calls onClick() when click was performed
// but wasn't reported. Condition can be extended for v.isClickable()
// or v.isEnabled() if needed. Returning false means that everything
// else behaves as before.
v.performClick();
}
return false;
}
});
Answer 4:
这是因为第一次轻触获得焦点到视图中。 接下来的水龙头触发点击。
如果你是动态膨胀的观点,这样做:
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false"
如果这不起作用,尝试应用它在父视图中。
Answer 5:
public class TestProject extends Activity implements OnClickListener {
TextView txtmsg;
EditText ed1, ed2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txtmsg = (TextView) findViewById(R.id.txtmsg);
ed1 = (EditText) findViewById(R.id.edt1);
ed2 = (EditText) findViewById(R.id.edt2);
ed1.setOnClickListener(this);
ed2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v==ed1){
txtmsg.setText("1");
Toast.makeText(this, "first",Toast.LENGTH_SHORT).show();
}
if(v==ed2){
txtmsg.setText("2");
Toast.makeText(this, "second",Toast.LENGTH_SHORT).show();
}
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="@+id/edt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ems="10" />
<EditText
android:id="@+id/edt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/edt1"
android:layout_marginTop="14dp"
android:ems="10" />
<TextView
android:id="@+id/txtmsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/edt2"
android:layout_below="@+id/edt2"
android:layout_marginRight="22dp"
android:layout_marginTop="160dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Answer 6:
我花了一分钟算出这个一个时候,这发生在我身上。 我ImageButton
有setOnClickListener
和onClick
似乎不火,然后我意识到这实际上是在我的XML布局另一元件下方,所以我把这个:
<RelativeLayout>
<ImageButton>
<LinearLayout></LinearLayout>
</RelativeLayout>
这个:
<RelativeLayout>
<LinearLayout></LinearLayout>
<ImageButton>
</RelativeLayout>
和突然的ImageButton没有被其他的布局重叠的,因为它是现在后来添加到父布局,现在在上面,屡试不爽。 祝你好运,总是充满乐趣,当基本的东西似乎突然停止工作
Answer 7:
避免使用FocusChangeListener
,因为它会运行不稳定的时候,你并不真的需要它(例如,当你进入一个活动)。 刚刚成立的OnTouchListener
与您一起OnClickListener
是这样的:
@Override
public boolean onTouch(View view, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
view.requestFocus();
break;
}
return false;
}
这将导致你的EditText首先接收焦点,而你onClick
正常工作的第一次。
Answer 8:
其最简单的方式与日期选取器工作。
private DatePickerDialog datePickerDialog;
EditText etJoiningDate;
etJoiningDate=(EditText)findViewById(R.id.etJoiningDate);
etJoiningDate.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
final Calendar cldr = Calendar.getInstance();
int day = cldr.get(Calendar.DAY_OF_MONTH);
int month = cldr.get(Calendar.MONTH);
int year = cldr.get(Calendar.YEAR);
// date picker dialog
datePickerDialog = new DatePickerDialog(TestActivity.this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
etJoiningDate.setText(dayOfMonth + "/" + (monthOfYear + 1) + "/" + year);
}
}, year, month, day);
datePickerDialog.show();
break;
}
return false;
}
});
文章来源: onClick event is not triggering | Android