#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); } } |
程序代码:
//这个是两个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>
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>