如何基于一个布尔值通过一个按钮访问两个不同的onclicklisteners
How to access two different onclicklisteners with one button based on a boolean
根据用户的选择,我想在按下按钮时打开一个 activity 或另一个。我还尝试仅设置一个 Listener 并在其中设置,具体取决于意图一个 activity 或另一个的布尔值,但这让我遇到了 "Variable is accessed from within inner class, needs to be declared at final " 错误。谢谢!
but1 = (Button) findViewById(R.id.start);
RadioButton selectionA = (RadioButton) findViewById(R.id.buttonSelectionA);
boolean VocabularySelected = selectionA.isChecked();
RadioButton selectionB = (RadioButton) findViewById(R.id.buttonSelectionB);
boolean MathSelected = selectionB.isChecked();
if (VocabularySelected) {
but1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent Vocabulary = new Intent(MainActivity.this, VocabularyActivity.class);
startActivity(Vocabulary);
}
});
}
if (MathSelected) {
but1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent Math = new Intent(MainActivity.this, MathActivity.class);
startActivity(Math);
}
});
}
为什么不使用相同的点击侦听器并在其中根据布尔值执行逻辑?
but1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (MathSelected) {
Intent Math = new Intent(MainActivity.this, MathActivity.class);
startActivity(Math);
} else if(VocabularySelected) {
Intent Vocabulary = new Intent(MainActivity.this, VocabularyActivity.class);
startActivity(Vocabulary);
}
}
});
一个视图只能分配一个 OnClickListener
,因此您需要检查从侦听器中采用哪条路径。
为避免有关访问非最终变量的错误,您可以以另一种方式执行检查。也许是这样的:
but1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
RadioButton selectionA = (RadioButton) findViewById(R.id.buttonSelectionA);
RadioButton selectionB = (RadioButton) findViewById(R.id.buttonSelectionB);
if (selectionA.isChecked()) {
Intent Vocabulary = new Intent(MainActivity.this, VocabularyActivity.class);
startActivity(Vocabulary);
else if (selectionB.isChecked()) {
Intent Math = new Intent(MainActivity.this, MathActivity.class);
startActivity(Math);
}
}
});
有很多方法可以做到这一点,这是我最喜欢的方法之一,通过在侦听器中引用视图本身:
but1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (((RadioButton) findViewById(R.id.buttonSelectionA)).isChecked()) {
Intent Vocabulary = new Intent(MainActivity.this, VocabularyActivity.class);
startActivity(Vocabulary);
} else if (((RadioButton) findViewById(R.id.buttonSelectionB)).isChecked()) {
Intent Math = new Intent(MainActivity.this, MathActivity.class);
startActivity(Math);
}
}
});
它不是最可读的,而是最短的。
您还可以将布尔变量声明为 class 的成员,并在一个侦听器中同时使用它们,如下所示:
public class YourActivity extends AppCompatActivity{
...
private boolean mVocabularyChecked, mMathChecked;
...
private void yourMethod() {
but1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mVocabularyChecked) {
Intent Vocabulary = new Intent(MainActivity.this, VocabularyActivity.class);
startActivity(Vocabulary);
} else if (mMathChecked) {
Intent Math = new Intent(MainActivity.this, MathActivity.class);
startActivity(Math);
}
}
});
}
根据用户的选择,我想在按下按钮时打开一个 activity 或另一个。我还尝试仅设置一个 Listener 并在其中设置,具体取决于意图一个 activity 或另一个的布尔值,但这让我遇到了 "Variable is accessed from within inner class, needs to be declared at final " 错误。谢谢!
but1 = (Button) findViewById(R.id.start);
RadioButton selectionA = (RadioButton) findViewById(R.id.buttonSelectionA);
boolean VocabularySelected = selectionA.isChecked();
RadioButton selectionB = (RadioButton) findViewById(R.id.buttonSelectionB);
boolean MathSelected = selectionB.isChecked();
if (VocabularySelected) {
but1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent Vocabulary = new Intent(MainActivity.this, VocabularyActivity.class);
startActivity(Vocabulary);
}
});
}
if (MathSelected) {
but1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent Math = new Intent(MainActivity.this, MathActivity.class);
startActivity(Math);
}
});
}
为什么不使用相同的点击侦听器并在其中根据布尔值执行逻辑?
but1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (MathSelected) {
Intent Math = new Intent(MainActivity.this, MathActivity.class);
startActivity(Math);
} else if(VocabularySelected) {
Intent Vocabulary = new Intent(MainActivity.this, VocabularyActivity.class);
startActivity(Vocabulary);
}
}
});
一个视图只能分配一个 OnClickListener
,因此您需要检查从侦听器中采用哪条路径。
为避免有关访问非最终变量的错误,您可以以另一种方式执行检查。也许是这样的:
but1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
RadioButton selectionA = (RadioButton) findViewById(R.id.buttonSelectionA);
RadioButton selectionB = (RadioButton) findViewById(R.id.buttonSelectionB);
if (selectionA.isChecked()) {
Intent Vocabulary = new Intent(MainActivity.this, VocabularyActivity.class);
startActivity(Vocabulary);
else if (selectionB.isChecked()) {
Intent Math = new Intent(MainActivity.this, MathActivity.class);
startActivity(Math);
}
}
});
有很多方法可以做到这一点,这是我最喜欢的方法之一,通过在侦听器中引用视图本身:
but1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (((RadioButton) findViewById(R.id.buttonSelectionA)).isChecked()) {
Intent Vocabulary = new Intent(MainActivity.this, VocabularyActivity.class);
startActivity(Vocabulary);
} else if (((RadioButton) findViewById(R.id.buttonSelectionB)).isChecked()) {
Intent Math = new Intent(MainActivity.this, MathActivity.class);
startActivity(Math);
}
}
});
它不是最可读的,而是最短的。
您还可以将布尔变量声明为 class 的成员,并在一个侦听器中同时使用它们,如下所示:
public class YourActivity extends AppCompatActivity{
...
private boolean mVocabularyChecked, mMathChecked;
...
private void yourMethod() {
but1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mVocabularyChecked) {
Intent Vocabulary = new Intent(MainActivity.this, VocabularyActivity.class);
startActivity(Vocabulary);
} else if (mMathChecked) {
Intent Math = new Intent(MainActivity.this, MathActivity.class);
startActivity(Math);
}
}
});
}