注册 登录
编程论坛 Android开发

新手写的一个关于和手机猜拳的小游戏练习。希望来人帮忙看看,谢谢。

ADust、 发布于 2014-09-22 17:31, 4169 次点击
程序代码:
//这个是两个layout文件

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas. android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="你选择出"
        android:textSize="30sp"
        android:gravity="center_horizontal"
        android:layout_marginTop="50dp"/>
    <Button
        android:id="@+id/btnST"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="石头"
        android:layout_marginTop="10dp"/>
    <Button
        android:id="@+id/btnJD"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="剪刀"
        android:layout_marginTop="10dp"/>
    <Button
        android:id="@+id/btnB"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="布"
        android:layout_marginTop="10dp"/>
    <Button
        android:id="@+id/btnSubmit"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="确定"
        android:layout_marginTop="30dp"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"/>
   
   
   

</LinearLayout>





activity_result.xml



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas. android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="电脑选择的是"
        android:textSize="30sp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        />
    <TextView
        android:id="@+id/textComselect"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="结果:"
        android:textSize="30sp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        />
    <TextView
        android:id="@+id/textResult"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"/>
   

</LinearLayout>
7 回复
#2
ADust、2014-09-22 17:32
这是两个java文件

MainActivity.java

package com.example.finger_guessing.activity;


import com.example.finger_guessing.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;

public class MainActivity extends Activity {
    private Button btnST,btnJD,btnB,btnSubmit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.setContentView(R.layout.activity_main);
        
        this.btnST=(Button)findViewById(R.id.btnST);
        this.btnJD=(Button)findViewById(R.id.btnJD);
        this.btnB=(Button)findViewById(R.id.btnB);
        
        this.btnSubmit.setOnClickListener(new ViewOcl());
        
    }
    private class ViewOcl implements View.OnClickListener{
        @Override
        public void onClick(View v){
            int iComPlay = (int)(Math.random()*3+1);
            String jieguo;
            Bundle b=new Bundle();
            switch(v.getId()){
            case R.id.btnST:
               
                if(iComPlay==1){
                    jieguo="平局";
                    b.putString("jieguo", jieguo);
                }
                else if(iComPlay==2){
                    jieguo="胜利";
                    b.putString("jieguo", jieguo);
                }
                else
                    jieguo="失败";
                    b.putString("jieguo", jieguo);
                break;
            case R.id.btnJD:
                if(iComPlay==1){
                    jieguo="失败";
                    b.putString("jieguo", jieguo);
                }
                else if(iComPlay==2){
                    jieguo="平局";
                    b.putString("jieguo", jieguo);
                }
                else
                    jieguo="胜利";
                    b.putString("jieguo", jieguo);
                break;
            case R.id.btnB:
                if(iComPlay==1){
                    jieguo="失败";
                    b.putString("jieguo", jieguo);
                }
                else if(iComPlay==2){
                    jieguo="胜利";
                    b.putString("jieguo", jieguo);
                }
                else
                    jieguo="平局";
                    b.putString("jieguo", jieguo);
                break;
            case R.id.btnSubmit:
                Intent intent=new Intent();
                intent.setClass(MainActivity.this, ResultActivity.class);
                startActivity(intent);
            }
        }
    }
   

}

ResultActivity.java


package com.example.finger_guessing.activity;


import com.example.finger_guessing.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.*;

public class ResultActivity extends Activity {
    private TextView textResult,textComselect;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_result);
        this.textComselect=(TextView)this.findViewById(R.id.textComselect);
        this.textResult=(TextView)this.findViewById(R.id.textResult);
        
        Intent intent=this.getIntent();
        Bundle extras=intent.getExtras();
        String jieguo=extras.getString("jieguo");
        
        this.textResult.setText(jieguo);
    }
   

}

#3
ADust、2014-09-22 17:33
这个是Manifest.xml

<manifest xmlns:android="http://schemas.
    package="com.example.finger_guessing"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        
         <activity android:name="com.example.finger_guessing.activity.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <activity android:name="com.example.finger_guessing.activity.ResultActivity"/>
    </application>
   
#4
ADust、2014-09-22 17:35
刚刚学习Android不久。就自己练习着写了一个小练习。一运行的时候程序就意外停止。找了好久也没有找到问题出在哪里。
#5
ADust、2014-09-22 17:35
希望大神们帮我看下问题出在哪里。 谢谢了。
#6
tlliqi2014-09-23 21:32
顶一下
#7
一个小菜鸡2014-09-28 21:51
package com.example.TestDemo;

import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MyActivity extends Activity {
    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        RadioGroup rg_group = (RadioGroup) findViewById(R.id.rg_group);
        final TextView tv_result = (TextView) findViewById(R.id.tv_result);
        String com_result = null;
        int iComPlay = (int)(Math.random()*3+1);
        switch (iComPlay){
            case 1:com_result = "石头";break;
            case 2:com_result = "剪刀";break;
            case 3:com_result = "布";break;
            default:break;
        }
        final String finalCom_result = com_result;
        System.out.println(finalCom_result);
        rg_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                int rb_id = radioGroup.getCheckedRadioButtonId();
                RadioButton rb = (RadioButton) MyActivity.this.findViewById(rb_id);

                String i_result = rb.getText().toString().trim();
                if(i_result.equals(finalCom_result)){
                    tv_result.setText("^-^恭喜猜对了");
                }else{
                    tv_result.setText("T—T,再接再厉");
                }
            }
        });
    }
}

布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
        android:background="@android:color/white">
    <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="猜拳小游戏"
            android:textColor="@android:color/black"
            />
    <RadioGroup android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:id="@+id/rg_group">
        <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content"
                     android:id="@+id/rb_stone"
                     android:textColor="@android:color/black"
                     android:text="石头"/>
        <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content"
                     android:id="@+id/rb_scissors"
                     android:textColor="@android:color/black"
                     android:text="剪刀"/>
        <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content"
                     android:id="@+id/rb_cloth"
                     android:textColor="@android:color/black"
                     android:text="布"/>
    </RadioGroup>
    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
              android:textColor="@android:color/black"
              android:text="结果:"/>
    <TextView android:layout_width="fill_parent" android:layout_height="wrap_content"
              android:textColor="@android:color/black"
            android:id="@+id/tv_result"/>

</LinearLayout>

ps:刚刚写的,你对着看下吧,一般listener写在Activity中作为内部类,结果的判断一般不使用activity,activity是作为一个view的作用,逻辑你可以写在control里面
#8
chenzhaun2015-04-01 22:34
顶一下......
1