我有2个if语句,如果
modechoiceId == R.id.textmode && sizechoiceId == R.id.normal -- 1
OR
modechoiceId == R.id.animationmode && sizechoieId = R.id.normal -- 2
(1)将使nextButton转到一个新的Activity,即textmode32by32.class和 (2)将使nextButton转到一个新的Activity,即animationmode32by32.class。
现在的问题是,这两个选项都会转到textmode32by32.class的同一页面。
问题出在RadioGroup吗?或者是if语句中的代码吗?
startPage.class
package tp.mp2014.dotmatrix;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
public class startPage extends Activity {
Button backButton, nextButton;
RadioGroup modeselection, sizeselection;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.selectionmode);
nextButton = (Button) findViewById(R.id.nextselection);
backButton = (Button) findViewById(R.id.backselection);
modeselection = (RadioGroup) findViewById(R.id.modeselection);
sizeselection = (RadioGroup) findViewById(R.id.sizeselection);
int modechoiceId = modeselection.getCheckedRadioButtonId();
int sizechoiceId = sizeselection.getCheckedRadioButtonId();
if(modechoiceId == R.id.textmode && sizechoiceId == R.id.normal){
nextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent nextSelection = new Intent(startPage.this, textmode32by32.class);
startActivity(nextSelection);
}
});
}
else if(modechoiceId == R.id.animationmode && sizechoiceId == R.id.normal){
nextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent nextSelection1 = new Intent(startPage.this, animationmode32by32.class);
startActivity(nextSelection1);
}
});
}
backButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent backSelection = new Intent(startPage.this, MainActivity.class);
startActivity(backSelection);
}
});
}
}
animationmode32by32.class
package tp.mp2014.dotmatrix;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class animationmode32by32 extends Activity {
Button nextBtn, backBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.animationmode32by32);
backBtn = (Button) findViewById(R.id.backBtn2);
backBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent backSelection = new Intent(animationmode32by32.this, startPage.class);
startActivity(backSelection);
}
});
}
}
textmode32by32.class
package tp.mp2014.dotmatrix;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.SeekBar;
public class textmode32by32 extends Activity {
Button nextBtn, backBtn;
RadioButton left, stagnant, right;
SeekBar scrollspeed;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.textmode32by32);
backBtn = (Button) findViewById(R.id.backBtn1);
backBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent backBtn = new Intent(textmode32by32.this, startPage.class);
startActivity(backBtn);
}
});
}
}
selectionmode.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/LinearLayout1H"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textmodeselection"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/textmodeselection"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textsizeselection"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:text="@string/textsizeselection"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<RadioGroup
android:id="@+id/sizeselection"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/modeselection"
android:layout_toRightOf="@+id/modeselection" >
<RadioButton
android:id="@+id/normal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/sizechoice1" />
<RadioButton
android:id="@+id/extended"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sizechoice2" />
</RadioGroup>
<RadioGroup
android:id="@+id/modeselection"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/LinearLayout1H" >
<RadioButton
android:id="@+id/textmode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/modechoice1" />
<RadioButton
android:id="@+id/animationmode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/modechoice2" />
<RadioButton
android:id="@+id/imagemode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/modechoice3" />
</RadioGroup>
<Button
android:id="@+id/backselection"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="@string/back" />
<Button
android:id="@+id/nextselection"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="@string/next" />
</RelativeLayout>
答案 0 :(得分:1)
试试这个,
nextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int modechoiceId = modeselection.getCheckedRadioButtonId();
int sizechoiceId = sizeselection.getCheckedRadioButtonId();
if (modechoiceId == R.id.textmode
&& sizechoiceId == R.id.normal) {
//first
} else if (modechoiceId == R.id.animationmode
&& sizechoiceId == R.id.normal) {
//second activity
}
}
});
答案 1 :(得分:0)
问题是当startPage
活动开始时,它会将按钮监听器设置为直接点击if(modechoiceId == R.id.textmode && sizechoiceId == R.id.normal)
<强>为什么??? 强>
因为每次检查另一个单选按钮时都不会更改它..
<强>溶液强>
将单选按钮的检查放在onclick监听器
中改变这一点:
if(modechoiceId == R.id.textmode && sizechoiceId == R.id.normal){
nextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent nextSelection = new Intent(startPage.this, textmode32by32.class);
startActivity(nextSelection);
}
});
}
else if(modechoiceId == R.id.animationmode && sizechoiceId == R.id.normal){
nextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent nextSelection1 = new Intent(startPage.this, animationmode32by32.class);
startActivity(nextSelection1);
}
});
}
以强>
nextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int modechoiceId = modeselection.getCheckedRadioButtonId();
int sizechoiceId = sizeselection.getCheckedRadioButtonId();
if(modechoiceId == R.id.textmode && sizechoiceId == R.id.normal){
Intent nextSelection1 = new Intent(startPage.this, animationmode32by32.class);
startActivity(nextSelection1);
}
else if(modechoiceId == R.id.animationmode && sizechoiceId == R.id.normal){
Intent backSelection = new Intent(startPage.this, MainActivity.class);
startActivity(backSelection);
}
}
});