gpt4 book ai didi

java - Android Studio 无法解决错误 setOnClickListener

转载 作者:行者123 更新时间:2023-12-01 09:34:24 30 4
gpt4 key购买 nike

嗨,我似乎无法构建这个。它指出它无法解析OnClickListener。 onClick 操作是在返回主 Activity 的后退按钮上执行的。

Button bnCompute = (Button) this.findViewById(R.id.bnCompute);
bnCompute.setOnClickListener(new View.OnClickListener());

{
@Override

public void onClick (View view){
Toast.makeText(MainActivity.this, "You Compute All!", Toast.LENGTH_LONG).show();


EditText etBeauty = (EditText) MainActivity.this.findViewById(R.id.etBeauty);
EditText etBody = (EditText) MainActivity.this.findViewById(R.id.etBody);
EditText etIntelligence = (EditText) MainActivity.this.findViewById(R.id.etIntelligence);

int total = Integer.parseInt(String.valueOf(etBeauty.getText())) + Integer.parseInt(String.valueOf(etBody.getText()))
+ Integer.parseInt(String.valueOf(etIntelligence.getText()));

Intent actSummary = new Intent(MainActivity.this, Score.class);
actSummary.putExtra("total", Integer.toString(total));
MainActivity.this.startActivity(actSummary);
}



}

最佳答案

您已在监听器范围之外实现了 onClick。它应该是这样的:

Button button = (Button) findViewById(R.id.button1);

//Your mistake is on this line.
button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "Button Clicked", Toast.LENGTH_SHORT).show();
}

});

关于java - Android Studio 无法解决错误 setOnClickListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39126066/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com