黑马程序员技术交流社区
标题:
请教变量问题
[打印本页]
作者:
doyxy
时间:
2014-3-5 15:45
标题:
请教变量问题
想想自己学学基础的android知识,做了个简单的石头剪刀布的小程序,点击了总是得不到结果,网上搜索对比别人的代码,发现是变量设置有问题,但是想不通为什么,请各位指点,谢谢
下面是部分代码,全部代码我放在后面吧,怕影响阅读
class ButtonOnClickListener implements OnClickListener {
//int choice1 = rga.getCheckedRadioButtonId(); 设置在这里时得不到结果
//int choice2 = rgb.getCheckedRadioButtonId();
@Override
public void onClick(View v) {
int choice1 = rga.getCheckedRadioButtonId(); //设置在这里时运行正常
int choice2 = rgb.getCheckedRadioButtonId();
if (choice1 != -1 && choice2 != -1) {
if (choice1 == astone.getId() && choice2 == bknife.getId()
|| choice1 == aknife.getId() && choice2 == bcloth.getId()
|| choice1 == acloth.getId() && choice2 == bstone.getId()) {
MyToast.show(v.getContext(), "A win!");
} else if (choice1 == astone.getId() && choice2 == bcloth.getId()
|| choice1 == acloth.getId() && choice2 == bknife.getId()
|| choice1 == aknife.getId() && choice2 == bstone.getId()) {
MyToast.show(v.getContext(), "B win!");
} else {
MyToast.show(v.getContext(), "Draw");
}
}
}
}
复制代码
作者:
doyxy
时间:
2014-3-5 15:48
本帖最后由 doyxy 于 2014-3-6 14:13 编辑
下面是全部代码
MainActivity.java
package fri.day.radiogame;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
class MyToast {
private static Toast mToast;
public static void show(Context context, CharSequence text) {
if (mToast == null) {
mToast = Toast.makeText(context, text, Toast.LENGTH_LONG);
} else {
mToast.setText(text);
}
mToast.show();
}
}
public class MainActivity extends Activity {
RadioGroup rga = null;
RadioButton astone = null;
RadioButton aknife = null;
RadioButton acloth = null;
RadioGroup rgb = null;
RadioButton bstone = null;
RadioButton bknife = null;
RadioButton bcloth = null;
Button button = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rga = (RadioGroup) findViewById(R.id.groupA);
astone = (RadioButton) findViewById(R.id.groupAStone);
aknife = (RadioButton) findViewById(R.id.groupAKnife);
acloth = (RadioButton) findViewById(R.id.groupACloth);
rgb = (RadioGroup) findViewById(R.id.groupB);
bstone = (RadioButton) findViewById(R.id.groupBStone);
bknife = (RadioButton) findViewById(R.id.groupBKnife);
bcloth = (RadioButton) findViewById(R.id.groupBCloth);
button = (Button) findViewById(R.id.button1);
ButtonOnClickListener listener = new ButtonOnClickListener();
button.setOnClickListener(listener);
}
class ButtonOnClickListener implements OnClickListener {
int choice1 = rga.getCheckedRadioButtonId();
int choice2 = rgb.getCheckedRadioButtonId();
@Override
public void onClick(View v) {
if (choice1 != -1 && choice2 != -1) {
if (choice1 == astone.getId() && choice2 == bknife.getId()
|| choice1 == aknife.getId() && choice2 == bcloth.getId()
|| choice1 == acloth.getId() && choice2 == bstone.getId()) {
MyToast.show(v.getContext(), "A win!");
} else if (choice1 == astone.getId() && choice2 == bcloth.getId()
|| choice1 == acloth.getId() && choice2 == bknife.getId()
|| choice1 == aknife.getId() && choice2 == bstone.getId()) {
MyToast.show(v.getContext(), "B win!");
} else {
MyToast.show(v.getContext(), "Draw");
}
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
复制代码
下面是activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RadioGroup
android:id="@+id/groupA"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/groupAStone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="石头" />
<RadioButton
android:id="@+id/groupAKnife"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="剪刀" />
<RadioButton
android:id="@+id/groupACloth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="布" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RadioGroup
android:id="@+id/groupB"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/groupBStone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="石头" />
<RadioButton
android:id="@+id/groupBKnife"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="剪刀" />
<RadioButton
android:id="@+id/groupBCloth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="布" />
</RadioGroup>
</LinearLayout>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="测试" />
</LinearLayout>
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2