[英]Have to click a button twice for it to work in Android Studio
So I am currently creating an app and one of the small things that have been bothering me is the fact that I have to click a button twice for it to work.所以我目前正在创建一个应用程序,一直困扰我的一件小事是我必须点击一个按钮两次才能工作。
This is my code and I can't see anything wrong with it:这是我的代码,我看不出有什么问题:
public void signUpButtonClickAction(View v){
Button signUpButtonClick = (Button) findViewById(R.id.signUpButton);
signUpButtonClick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Signup.class));
}
});
}
xml code for my button:我的按钮的 xml 代码:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/signUps"
android:id="@+id/signUpButton"
android:layout_marginBottom="38dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="signUpButtonClickAction"/>
It is probably a small fix but even I can't spot this bug这可能是一个小修复,但即使我也无法发现这个错误
Solution解决方案
Remove the line android:onClick="signUpButtonClickAction"
and add删除行
android:onClick="signUpButtonClickAction"
并添加
Button signUpButtonClick = (Button) findViewById(R.id.signUpButton);
signUpButtonClick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Signup.class));
}
});
to the onCreate
method of your activity or the onCreateView
method of your fragment.到
onCreate
您活动的方法或onCreateView
您的片段方法。
Alternative Solution替代方案
Alternatively, change the code to this或者,将代码更改为此
public void signUpButtonClickAction(View v) {
startActivity(new Intent(MainActivity.this, Signup.class));
}
Explanation解释
The line android:onClick="signUpButtonClickAction"
in the xml is causing an internal call to signUpButtonClick.setOnClickListener(), so you don't have to set up an onClickListener in the signUpButtonClickAction
again. xml 中的
android:onClick="signUpButtonClickAction"
导致对 signUpButtonClick.setOnClickListener() 的内部调用,因此您不必再次在signUpButtonClickAction
中设置signUpButtonClickAction
。
Initializing multiple buttons初始化多个按钮
private void initializeButtons() {
Button signUpButtonClick = (Button) findViewById(R.id.signUpButton);
signUpButtonClick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Signup.class));
}
});
Button anotherButton = (Button) findViewById(R.id.anotherButton);
anotherButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("TAG", "Clicked on another button!");
}
});
}
Now simply call initializeButtons()
from the onCreate
method of your activity.现在只需从活动的
onCreate
方法调用initializeButtons()
即可。
The problem is that you are setting two times a onClick action.问题是您设置了两次 onClick 操作。 In your xml code you have just asign an onClick() to your button, you don't need to setOnClickListener() inside the
signUpButtonClickAction(View v)
.在您的 xml 代码中,您只需将 onClick() 分配给您的按钮,您无需在
signUpButtonClickAction(View v)
内设置signUpButtonClickAction(View v)
。 You have two options:您有两个选择:
Leave the xml file like it is and inside signUpButtonClickAction(View v)
do :将 xml 文件保持
signUpButtonClickAction(View v)
,在signUpButtonClickAction(View v)
执行以下操作:
public void signUpButtonClickAction(View v){
startActivity(new Intent(MainActivity.this, Signup.class));
}
OR或者
Remove the onClick of your xml file:删除您的 xml 文件的 onClick:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/signUps"
android:id="@+id/signUpButton"
android:layout_marginBottom="38dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
/>
And do this in your Activity:并在您的活动中执行此操作:
Button yourButton = (Button) findViewById(R.id.signUpButton);
yourButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Signup.class));
}
});
The cause of the problem is : onclick()
and onClickListener
are literally the same!问题的原因是:
onclick()
和onClickListener
字面上是一样的! And you are implementing both, the end result is you'll need to press the button twice to start the Activity!并且您正在实现两者,最终结果是您需要按两次按钮才能启动活动!
FIX:使固定:
The solution to your problem is :您的问题的解决方案是:
1: 1:
public void signUpButtonClickAction(View v)
{
startActivity(new Intent(MainActivity.this, Signup.class));
}
2: 2:
Button signUpButtonClick = (Button) findViewById(R.id.signUpButton);
signUpButtonClick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Signup.class));
}
});
as mcwise said android:onClick="signUpButtonClickAction" and signUpButtonClick.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(MainActivity.this, Signup.class)); } });正如 mcwise 所说 android:onClick="signUpButtonClickAction" 和 signUpButtonClick.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(MainActivity.this, Signup.class)); } }) ; does the same thing.
做同样的事情。 so you have to go with one of them.
所以你必须和其中一个一起去。 Having the two is causing the problem
有两个是导致问题的原因
For whom it may concern: I had the same issue but none of the solutions above solved it.可能涉及的人:我遇到了同样的问题,但上述解决方案均未解决。 For some reason I cannot understand, I had in my button this line of code:
出于某种原因,我无法理解,我的按钮中有这行代码:
android:textIsSelectable="true"
Deleting this attribute from the button makes it work.从按钮中删除此属性使其工作。
This obviously made the first click to select the text, and the second click triggered the onClick button.这显然是第一次点击选择文本,第二次点击触发了 onClick 按钮。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.