必须单击两次按钮才能在Android Studio中工作 [英] Have to click a button twice for it to work in Android Studio

查看:334
本文介绍了必须单击两次按钮才能在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代码:

xml code for my button:

<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

推荐答案

解决方案

删除行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));
    }
});

转到活动的onCreate方法或片段的onCreateView方法.

to the onCreate method of your activity or the onCreateView method of your fragment.

替代解决方案

或者,将代码更改为此

public void signUpButtonClickAction(View v) { 
    startActivity(new Intent(MainActivity.this, Signup.class));
}

说明

xml中的android:onClick="signUpButtonClickAction"行引起对signUpButtonClick.setOnClickListener()的内部调用,因此您不必再次在signUpButtonClickAction中设置onClickListener.

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.

初始化多个按钮

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!");
        }
    });
}

现在只需从活动的onCreate方法调用initializeButtons().

Now simply call initializeButtons() from the onCreate method of your activity.

这篇关于必须单击两次按钮才能在Android Studio中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆