第一次按下按钮未被听众捕捉到
First button press not caught by listener
第一次按下 "edit info" 按钮时没有任何反应,但随后的几次都正常。有很多类似的问题,我已经通过并尝试了所有我能做的事情都没有成功。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sharedPrefs = getSharedPreferences(USER_PREFERENCES, MODE_PRIVATE);
//call function to load previous values, no intteraction with button
final Button myButton = findViewById(R.id.button);
myButton.setTag(1);
myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final int status =(Integer) v.getTag();
if(status == 1) {
myButton.setText(getString(R.string.edit_info));
setEditable(false);
v.setTag(0);
} else {
myButton.setText(getString(R.string.save_info));
setEditable(true);
v.setTag(1);
}
}
});
//proposed solution from other question, see note bellow (*)
myButton.setOnTouchListener(new View.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.
myButton.performClick();
}
return false;
}
});
}
(*) 来自 this question Matěj Hrazdíra 建议添加 OnTouchListner 但这样做会使按钮永远不会切换回来。
这里是activity_main.xml
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="@string/edit_info" />
来自 xml 的按钮已有文本 "@string/edit_info"
。
在 onCreate()
中,您将其标记设置为 1。因此侦听器将执行:
myButton.setText(getString(R.string.edit_info));
setEditable(false);
v.setTag(0);
所以文字没有变化。
之后,随后的点击将更改其文本。
我不知道你到底想要什么,但如果在 onCreate()
中将其标签设置为 0,你将看到从第一次点击开始的变化。
第一次按下 "edit info" 按钮时没有任何反应,但随后的几次都正常。有很多类似的问题,我已经通过并尝试了所有我能做的事情都没有成功。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sharedPrefs = getSharedPreferences(USER_PREFERENCES, MODE_PRIVATE);
//call function to load previous values, no intteraction with button
final Button myButton = findViewById(R.id.button);
myButton.setTag(1);
myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final int status =(Integer) v.getTag();
if(status == 1) {
myButton.setText(getString(R.string.edit_info));
setEditable(false);
v.setTag(0);
} else {
myButton.setText(getString(R.string.save_info));
setEditable(true);
v.setTag(1);
}
}
});
//proposed solution from other question, see note bellow (*)
myButton.setOnTouchListener(new View.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.
myButton.performClick();
}
return false;
}
});
}
(*) 来自 this question Matěj Hrazdíra 建议添加 OnTouchListner 但这样做会使按钮永远不会切换回来。
这里是activity_main.xml
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="@string/edit_info" />
来自 xml 的按钮已有文本 "@string/edit_info"
。
在 onCreate()
中,您将其标记设置为 1。因此侦听器将执行:
myButton.setText(getString(R.string.edit_info));
setEditable(false);
v.setTag(0);
所以文字没有变化。
之后,随后的点击将更改其文本。
我不知道你到底想要什么,但如果在 onCreate()
中将其标签设置为 0,你将看到从第一次点击开始的变化。