| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4169 人关注过本帖
标题:新手写的一个关于和手机猜拳的小游戏练习。希望来人帮忙看看,谢谢。
只看楼主 加入收藏
ADust、
Rank: 2
等 级:论坛游民
帖 子:21
专家分:57
注 册:2013-1-10
结帖率:50%
收藏
已结贴  问题点数:20 回复次数:7 
新手写的一个关于和手机猜拳的小游戏练习。希望来人帮忙看看,谢谢。
程序代码:
//这个是两个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>
搜索更多相关主题的帖子: layout 小游戏 version vertical android 
2014-09-22 17:31
ADust、
Rank: 2
等 级:论坛游民
帖 子:21
专家分:57
注 册:2013-1-10
收藏
得分:0 
这是两个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);
    }
   

}

2014-09-22 17:32
ADust、
Rank: 2
等 级:论坛游民
帖 子:21
专家分:57
注 册:2013-1-10
收藏
得分:0 
这个是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>
   
2014-09-22 17:33
ADust、
Rank: 2
等 级:论坛游民
帖 子:21
专家分:57
注 册:2013-1-10
收藏
得分:0 
刚刚学习Android不久。就自己练习着写了一个小练习。一运行的时候程序就意外停止。找了好久也没有找到问题出在哪里。
2014-09-22 17:35
ADust、
Rank: 2
等 级:论坛游民
帖 子:21
专家分:57
注 册:2013-1-10
收藏
得分:0 
希望大神们帮我看下问题出在哪里。 谢谢了。
2014-09-22 17:35
tlliqi
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:204
帖 子:15453
专家分:65956
注 册:2006-4-27
收藏
得分:10 
顶一下
2014-09-23 21:32
一个小菜鸡
Rank: 2
等 级:论坛游民
帖 子:3
专家分:23
注 册:2014-7-31
收藏
得分:10 
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里面
2014-09-28 21:51
chenzhaun
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2015-4-1
收藏
得分:0 
顶一下......
2015-04-01 22:34
快速回复:新手写的一个关于和手机猜拳的小游戏练习。希望来人帮忙看看,谢谢。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016367 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved