RadioButton利用三个单选按钮写一个石头剪刀布的小项目、开辟子线程、和Handler

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>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值