我的代码出了什么问题? if if语句为10按钮10活动。我上传了我的代码图片。 我的代码出了什么问题? if if语句为10按钮10活动。我上传了我的代码图片。?
public void showGreeting (View view) {
String button_text;
button_text = ((Button) view).getText().toString();
if (button_text.equals("Chapter 2 and 6")) {
} else (button_text.equals("Chapter 7 Part 1"))
{
Intent intent = new Intent(this, SecindActivity.class);
startActivity(intent);
}
else (button_text.equals("Chapter 7 Part 2"))
{
Intent intent = new Intent(this, ThirdActivity.class);
startActivity(intent);
}
else (button_text.equals("Chapter 8 and Chapter 9 Part 1"))
{
Intent intent = new Intent(this, FourthActivity.class);
startActivity(intent);
}
else (button_text.equals("Chapter 9 Part 2"))
{
Intent intent = new Intent(this, FifthActivity.class);
startActivity(intent);
}
else (button_text.equals("Chapter 10"))
{
Intent intent = new Intent(this, SixthActivity.class);
startActivity(intent);
}
else (button_text.equals("Chapter 11 Part 1"))
{
Intent intent = new Intent(this, SeventhActivity.class);
startActivity(intent);
}
else (button_text.equals("Chapter 11 Part 2 and Chapter 12 Part 1"))
{
Intent intent = new Intent(this, EighthActivity.class);
startActivity(intent);
}
else (button_text.equals("Chapter 12 Part 2"))
{
Intent intent = new Intent(this, NinthActivity.class);
startActivity(intent);
}
else if (button_text.equals("Credit")) {
Intent intent = new Intent(this, TenthActivity.class);
startActivity(intent);
}
}
答案 0 :(得分:2)
试试这个:
isUserAuthenticated
public class MainActivity extends ActionBarActivity {
private Button button1;
//create the 10 buttons here
public void showGreetings(Button button) {
Intent intent;
switch (button.getId()) {
case R.id.button:
intent = new Intent(this, SecindActivity.class);
startActivity(intent);
break;
case R.id.button2:
intent = new Intent(this, ThirdActivity.class);
startActivity(intent);
break;
case R.id.button3:
intent = new Intent(this, FourthActivity.class);
startActivity(intent);
break;
case R.id.button4:
intent = new Intent(this, FifthActivity.class);
startActivity(intent);
break;
case R.id.button5:
intent = new Intent(this, SixthActivity.class);
startActivity(intent);
break;
case R.id.button6:
intent = new Intent(this, SeventhActivity.class);
startActivity(intent);
break;
case R.id.button7:
intent = new Intent(this, EighthActivity.class);
startActivity(intent);
break;
case R.id.button8:
intent = new Intent(this, NinthActivity.class);
startActivity(intent);
break;
case R.id.button9:
intent = new Intent(this, TenthActivity.class);
startActivity(intent);
break;
case R.id.button10:
intent = new Intent(this, Eleventh_Activity.class);
startActivity(intent);
break;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showGreetings((Button) view);
}
});
button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showGreetings((Button) view);
}
});
//do the same with the other 9 buttons
}
中获取的ID是您为xml中的按钮指定的ID。您可以用这种方式进行比较,而不是使用很多其他if语句。
否则,您也可以执行以下操作,使其更容易,更清晰。
b.getId()
你需要再做一次这样的活动:
public class MainActivity extends ActionBarActivity {
private Button button1;
private Button button2;
//create the other 8 buttons here
public void showGreetings(Button button) {
Intent intent = new Intent(this, SecindActivity.class);
switch (button.getId()) {
case R.id.button:
intent.putExtra("POSITION", 1);
startActivity(intent);
break;
case R.id.button2:
intent.putExtra("POSITION", 2);
startActivity(intent);
break;
case R.id.button3:
intent.putExtra("POSITION", 3);
startActivity(intent);
break;
case R.id.button4:
intent.putExtra("POSITION", 4);
startActivity(intent);
break;
case R.id.button5:
intent.putExtra("POSITION", 5);
startActivity(intent);
break;
case R.id.button6:
intent.putExtra("POSITION", 6);
startActivity(intent);
break;
case R.id.button7:
intent.putExtra("POSITION", 7);
startActivity(intent);
break;
case R.id.button8:
intent.putExtra("POSITION", 8);
startActivity(intent);
break;
case R.id.button9:
intent.putExtra("POSITION", 9);
startActivity(intent);
break;
case R.id.button10:
intent.putExtra("POSITION", 10);
startActivity(intent);
break;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showGreetings((Button) view);
}
});
button2 = (Button) findViewById(R.id.button);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showGreetings((Button) view);
}
});
//do the same with the other 9 buttons
}
}