我是初学者,想要为Java中的按钮制作一个点击监听器,你能帮帮我吗?

huangapple go评论70阅读模式
英文:

I am a beginner trying to make a click listener for a button in java, can you help me?

问题

我正在尝试在Android Studio中编译我的代码,代码如下:

choice1.setOnClickListener(new View.OnClickListener(){
        public void onClick(View view)
        {
            if(choice1.getText().equals(result))
                showToastMessage(true);
            else
                showToastMessage(false);
        }
    });

Android Studio指出所有行都是非法的,无法在Android模拟器中运行,因为无法编译。有人能帮我看看出了什么问题吗?

编辑:以下是我在这个特定实例中使用的代码:

public class FullscreenActivity extends AppCompatActivity {
    private String result = "New York";
    private Button choice1 = (Button)findViewById(R.id.choice1);
    private Button choice2 = (Button)findViewById(R.id.choice2);
    private Button choice3 = (Button)findViewById(R.id.choice3);

    @Override protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fullscreen);
    
        choice1.setOnClickListener(new View.OnClickListener(){
            public void onClick(View view)
            {
                if(choice1.getText().equals(result))
                    showToastMessage(true);
                else
                    showToastMessage(false);
            }
        });

        choice2.setOnClickListener(new View.OnClickListener(){
            public void onClick(View view) {
                if(choice2.getText().equals(result))
                    showToastMessage(true);
                else
                    showToastMessage(false);
            }
        });

        choice3.setOnClickListener(new View.OnClickListener(){
            public void onClick(View view) {
                if(choice3.getText().equals(result))
                    showToastMessage(true);
                else
                    showToastMessage(false);
            }
        });
    }

    private void showToastMessage(boolean showToastMessage) {
        if(showToastMessage)
            Toast.makeText(this, "Correct", Toast.LENGTH_SHORT).show();
        else
            Toast.makeText(this, "Wrong", Toast.LENGTH_SHORT).show();
    }
}

我不确定我学习的代码是旧的,还是我犯了错误,或者其他情况,我只是想尽量学好它,所以才在这里询问。感谢所有回复的人! <3

英文:

I am trying to compile my code in Android Studio, code is here:

choice1.setOnClickListener(new View.OnClickListener(){
        public void onClick(View view)
        {
            if(choice1.getText().equals(result))
                showToastMessage(true);
            else
                showToastMessage(false);
        }
    });

Android Studio is saying that all of the lines are illegal and won't run in the android emulator because it cannot be compiled. Can someone help me with what is going wrong?

EDIT: Here is the code that I am using in this specific instance:

public class FullscreenActivity extends AppCompatActivity {
    private String result = &quot;New York&quot;;
    private Button choice1 = (Button)findViewById(R.id.choice1);
    private Button choice2 = (Button)findViewById(R.id.choice2);
    private Button choice3 = (Button)findViewById(R.id.choice3);

    @Override protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fullscreen);
    }

    choice1.setOnClickListener(new View.OnClickListener(){
        public void onClick(View view)
        {
            if(choice1.getText().equals(result))
                showToastMessage(true);
            else
                showToastMessage(false);
        }
    });

    choice2.setOnClickListener(new View.OnClickListener(){
        public void onClick(View view) {
            if(choice2.getText().equals(result))
                showToastMessage(true);
            else
                showToastMessage(false);
        }
    });

    choice3.setOnClickListener(new View.OnClickListener(){
        public void onClick(View view) {
            if(choice3.getText().equals(result))
                showToastMessage(true);
            else
                showToastMessage(false);
        }
    });
}

    private void showToastMessage(boolean showToastMessage)
    {
            if(showToastMessage)
                Toast.makeText(this, &quot;Correct&quot;, toast.LENGTH_SHORT).show();
            else
                Toast.makeText(this, &quot;Wrong&quot;, Toast.LENGTH_SHORT).show();
    }

It's just that I don't know if the code I am learning is old, I made a mistake or whatever the case may be, I just want to learn it as best I can, hence why I'm asking. Thanks to everyone who has replied! <3

答案1

得分: 2

你必须在生命周期回调函数之一中放置一个点击监听器。如果是一个活动(activity),将代码移动到 onCreate() 方法中。

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.filters);

    choice1.setOnClickListener(new View.OnClickListener(){
        public void onClick(View view)
        {
            if(choice1.getText().equals(result))
                showToastMessage(true);
            else
                showToastMessage(false);
        }
    });
}

如果是一个片段(fragment),请将其放置在 onViewCreated() 方法中。

英文:

You have to put a click listener in one of the lifecycle callback. If it is an activity,
move the code to onCreate() method.

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.filters); 
choice1.setOnClickListener(new View.OnClickListener(){
public void onClick(View view)
{
if(choice1.getText().equals(result))
showToastMessage(true);
else
showToastMessage(false);
}
});
}

if it's a fragment, put it in onViewCreated().

huangapple
  • 本文由 发表于 2020年5月2日 12:18:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/61554397.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定