英文:
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 = "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();
}
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()
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论