package xena.act;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.os.Build;
public class MainActivity extends Activity implements OnClickListener {
private int time = 200;
private Button begin, stop;
private RadioGroup radioGroup_user1, radioGroup_user2;
private boolean isBegin = false; //表示是否开始
private void init() {
this.begin = (Button) this.findViewById(R.id.begin);
this.stop = (Button) this.findViewById(R.id.stop);
this.radioGroup_user1 = (RadioGroup) this
.findViewById(R.id.radioGroup_user1);
this.radioGroup_user2 = (RadioGroup) this
.findViewById(R.id.radioGroup_user2);
this.begin.setOnClickListener(this);
this.stop.setOnClickListener(this);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.init();
}
private Handler handler = new Handler() {
public void handleMessage(Message msg) {//主线程
if(isBegin == false) {
return;
}
RadioButton radioButton = (RadioButton) msg.obj;
radioButton.setChecked(true);
}
};
@Override
public void onClick(View v) {
if (v.getId() == R.id.begin) {
isBegin = true;
new Thread() {
public void run() {//子线程
for(int i=0; i<10000; i++) {
RadioButton radioButton = (RadioButton) radioGroup_user2.getChildAt(i%3);
Message msg = new Message();
msg.obj = radioButton;
handler.sendMessage(msg);
if(isBegin == false) {
break;
}
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
} else if (v.getId() == R.id.stop) {
isBegin = false;
//得到选中的radioButton的id
int user1_radioButton_id = radioGroup_user1.getCheckedRadioButtonId();
int user2_radioButton_id = radioGroup_user2.getCheckedRadioButtonId();
RadioButton user1_radioButton = (RadioButton) this.findViewById(user1_radioButton_id);
RadioButton user2_radioButton = (RadioButton) this.findViewById(user2_radioButton_id);
String user1_str = user1_radioButton.getText().toString();
String user2_str = user2_radioButton.getText().toString();
if("剪刀".equals(user1_str)) {
if("剪刀".equals(user2_str)) {
Toast.makeText(this, "平局", Toast.LENGTH_LONG).show();
}else if("石头".equals(user2_str)) {
Toast.makeText(this,"success", Toast.LENGTH_LONG).show();
}else if("布".equals(user2_str)) {
Toast.makeText(this,"失败", Toast.LENGTH_LONG).show();
}
}
}
}
}
<pre name="code" class="html"><LinearLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioGroup
android:id="@+id/radioGroup_user1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
<RadioButton
android:id="@+id/jianDao_user1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="剪刀" />
<RadioButton
android:id="@+id/shitou_user1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="石头" />
<RadioButton
android:id="@+id/bu_user1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="布" />
</RadioGroup>
<RadioGroup
android:id="@+id/radioGroup_user2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
<RadioButton
android:id="@+id/jianDao_user2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="剪刀" />
<RadioButton
android:id="@+id/shitou_user2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="石头" />
<RadioButton
android:id="@+id/bu_user2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="布" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Button
android:layout_weight="1"
android:id="@+id/begin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始" />
<Button
android:layout_weight="1"
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止" />
</LinearLayout>
</LinearLayout>