編輯:關於Android編程
本文實例為大家分享了Android猜拳小游戲,供大家參考,具體內容如下
簡單的 頁面跳轉 和 點擊事件 的實現...
--> AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.dragon.android.fight" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="15" 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.dragon.android.fight.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.dragon.android.fight.OtherActivity"> </activity> </application> </manifest> AndroidManifest
--> strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">fight</string> <string name="hello_world">Hello world!</string> <string name="action_settings">Settings</string> <string name="player1">甲方</string> <string name="player2">乙方</string> <string name="choose1">石頭</string> <string name="choose2">剪刀</string> <string name="choose3">布</string> <string name="sure">出拳</string> <string name="again">再來一局</string> </resources> --> fragment_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffff" tools:context="com.dragon.android.fight.MainActivity$PlaceholderFragment" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="36dp" android:text="@string/player1" android:textSize="30sp" /> <RadioGroup android:id="@+id/radioGroup1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" > <RadioButton android:id="@+id/radio0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="@string/choose1" /> <RadioButton android:id="@+id/radio1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/choose2" /> <RadioButton android:id="@+id/radio2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/choose3" /> </RadioGroup> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/radioGroup1" android:layout_below="@+id/radioGroup1" android:layout_marginTop="14dp" android:text="@string/sure" /> <ImageView android:id="@+id/imageView1" android:layout_width="120dp" android:layout_height="120dp" android:layout_above="@+id/radioGroup1" android:layout_below="@+id/textView1" android:layout_centerHorizontal="true" android:src="@drawable/b" /> </RelativeLayout>
--> activity_other.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="#ffffff" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="36dp" android:text="@string/player2" android:textSize="30sp" /> <RadioGroup android:id="@+id/radioGroup1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" > <RadioButton android:id="@+id/radio0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="@string/choose1" /> <RadioButton android:id="@+id/radio1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/choose2" /> <RadioButton android:id="@+id/radio2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/choose3" /> </RadioGroup> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/radioGroup1" android:layout_below="@+id/radioGroup1" android:layout_marginTop="14dp" android:text="@string/sure" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView1" android:layout_below="@+id/button1" android:visibility="invisible" android:layout_marginTop="14dp"/> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView2" android:layout_centerHorizontal="true" android:visibility="invisible" android:text="@string/again" /> <ImageView android:id="@+id/imageView1" android:layout_width="120dp" android:layout_height="120dp" android:layout_above="@+id/radioGroup1" android:layout_below="@+id/textView1" android:layout_centerHorizontal="true" android:src="@drawable/a" /> </RelativeLayout>
--> MainActivity
package com.dragon.android.fight; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; import android.widget.RadioButton; import android.widget.RadioGroup; public class MainActivity extends Activity { // 設置一個靜態變量,用於關閉Activity public static MainActivity instance = null; private RadioGroup radioGroup1; private Button button1; private ImageView imageView1; @Override protected void onCreate(Bundle savedInstanceState) { // 代表當前的Activity instance = this; super.onCreate(savedInstanceState); setContentView(R.layout.fragment_main); radioGroup1 = (RadioGroup) findViewById(R.id.radioGroup1); // 設置圖片透明 // imageView1 = (ImageView) findViewById(R.id.imageView1); // imageView1.getBackground().setAlpha(100); button1 = (Button) findViewById(R.id.button1); button1.setOnClickListener(new MyButtonListener()); } class MyButtonListener implements OnClickListener { @Override public void onClick(View v) { // 得到選中的RadioButton RadioButton radioButton = (RadioButton) findViewById(radioGroup1 .getCheckedRadioButtonId()); String radioText = radioButton.getText().toString(); Intent intent = new Intent(); intent.putExtra("checked", radioText); intent.setClass(MainActivity.this, OtherActivity.class); startActivity(intent); } } }
--> OtherActivity
package com.dragon.android.fight; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.TextView; public class OtherActivity extends Activity { private RadioGroup radioGroup1; private Button button1; private TextView textView2; private RadioButton radioButton; private Button button2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_other); radioGroup1 = (RadioGroup) findViewById(R.id.radioGroup1); button1 = (Button) findViewById(R.id.button1); textView2 = (TextView) findViewById(R.id.textView2); button2 = (Button) findViewById(R.id.button2); button1.setOnClickListener(new MyButtonListener()); button2.setOnClickListener(new MyButtonListener1()); } class MyButtonListener implements OnClickListener { @Override public void onClick(View v) { radioButton = (RadioButton) findViewById(radioGroup1 .getCheckedRadioButtonId()); String buttonText = radioButton.getText().toString(); Intent intent = getIntent(); String checked = intent.getStringExtra("checked"); // 設置View為可見 textView2.setVisibility(View.VISIBLE); button2.setVisibility(View.VISIBLE); String msg = "甲出:" + checked + "\n" + "乙出:" + buttonText + "\n"; if (buttonText.equals(checked)) { textView2.setText(msg + "平局"); } if (buttonText.equals("石頭")) { if (checked.equals("剪刀")) { textView2.setText(msg + "乙方贏"); } else if (checked.equals("布")) { textView2.setText(msg + "甲方贏"); } } if (buttonText.equals("剪刀")) { if (checked.equals("布")) { textView2.setText(msg + "乙方贏"); } else if (checked.equals("石頭")) { textView2.setText(msg + "甲方贏"); } } if (buttonText.equals("布")) { if (checked.equals("石頭")) { textView2.setText(msg + "乙方贏"); } else if (checked.equals("剪刀")) { textView2.setText(msg + "甲方贏"); } } } } class MyButtonListener1 implements OnClickListener { @Override public void onClick(View arg0) { Intent intent = new Intent(); intent.setClass(OtherActivity.this, MainActivity.class); finish(); // 關閉指定Activity MainActivity.instance.finish(); startActivity(intent); } } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持本站。
本文實例講述了Android編程開發之RadioGroup用法。分享給大家供大家參考,具體如下:RadioGroup 有時候比較有用.主要特征是給用戶提供多選一機制。Ma
在Android中經常會遇到需要使用Dialog風格彈出框的activity,首先我們可能會首先想到的是在XML布局文件中設置android:layout_height=
0. 前言 Android動畫是面試的時候經常被問到的話題。我們都知道Android動畫分為三類:View動畫、幀動畫和屬性動畫。先對這三種動畫做一個概述:V
先看效果圖:1.顯示三個頁面的Activity 用view pager去加載三個fragment實現,控制點點點的切換,監聽view pager的切換,控制fragmen