java;
package EX04_24.txt;
import EX04_24.txt.R.string;
import android.R.integer;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
public class EX04_24 extends Activity {
private TextView mTextView;
private RadioGroup mRadioGroup;
private RadioButton mRadioButton1;
private RadioButton mRadioButton2;
private RadioButton mRadioButton3;
private Button mButton1;
private Button mButton2;
private int[] choices = null;
private String[] items = null;
private int mchoice;
private int mCount = 0;
private boolean mNotify = false;
private String mStr = "";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initApp();
}
private void initApp() {
// TODO Auto-generated method stub
mTextView = (TextView)findViewById(R.id.TextView01);
mRadioGroup = (RadioGroup)findViewById(R.id.RadioGroup01);
mRadioButton1 = (RadioButton)findViewById(R.id.RadioButton01);
mRadioButton2 = (RadioButton)findViewById(R.id.RadioButton02);
mRadioButton3 = (RadioButton)findViewById(R.id.RadioButton03);
mButton1 = (Button)findViewById(R.id.Button01);
mButton2 = (Button)findViewById(R.id.Button02);
choices = new int[]
{
mRadioButton1.getId(),
mRadioButton2.getId(),
mRadioButton3.getId()
};
items = new String[]
{
getResources().getString(R.string.app_choice1),
getResources().getString(R.string.app_choice2),
getResources().getString(R.string.app_choice3)
};
init_mchoice();
mRadioGroup.setOnCheckedChangeListener(myChecked);
mButton1.setOnClickListener(myAnswer);
mButton2.setOnClickListener(myClear);
}
private void init_mchoice() {
// TODO Auto-generated method stub
mchoice = choices[(int) (Math.random()*3)];
for (int i = 0; i < choices.length; i++) {
if (mchoice == choices[i]) {
mStr = items[i];
break;
}
}
}
OnClickListener myClear =
new Button.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mTextView.setText(getResources().getString(R.string.str_hellow));
mNotify = false;
mRadioGroup.clearCheck();
mCount = 0;
init_mchoice();
}
};
OnClickListener myAnswer =
new Button.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (mNotify) {
mTextView.setText(getResources().getString(R.string.str_correct)
+ mStr);
mNotify = false;
mRadioGroup.clearCheck();
mCount = 0;
} else {
mCount++;
mTextView.setText(getResources().getString(R.string.str_error)
+ Integer.toString(mCount) + ")");
Animation am = new AnimationUtils().
loadAnimation(EX04_24.this, R.anim.my_anim);
v.startAnimation(am);
}
init_mchoice();
}
};
OnCheckedChangeListener myChecked =
new RadioGroup.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if (checkedId == mchoice) {
mNotify = true;
} else {
mNotify = false;
}
}
};
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/TextView01"
android:textSize = "18sp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/str_hellow"
/>
<TextView
android:text="@string/app_title"
android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<RadioGroup
android:id="@+id/RadioGroup01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:text="@string/app_choice1"
android:id="@+id/RadioButton01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</RadioButton>
<RadioButton
android:text="@string/app_choice2"
android:id="@+id/RadioButton02"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</RadioButton>
<RadioButton
android:text="@string/app_choice3"
android:id="@+id/RadioButton03"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</RadioButton>
</RadioGroup>
<LinearLayout
android:id="@+id/LinearLayout01"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:text="回答"
android:id="@+id/Button01"
android:layout_marginLeft = "20dip"
android:layout_width="100dip"
android:layout_height="wrap_content">
</Button>
<Button
android:text="清除"
android:id="@+id/Button02"
android:layout_marginLeft = "50dip"
android:layout_width="100dip"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
</LinearLayout>
string.xml
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<resources>
<string name="hello">Hello World, EX04_24!</string>
<string name="app_name">EX04_24</string>
<string name="str_hellow">欢迎进入猜猜我在想什么</string>
<string name="str_correct">你猜对了,我在想:</string>
<string name="str_error">猜错啦,再猜猜看...(</string>
<string name="app_title">请用念力猜猜我现在在想什么?</string>
<string name="app_choice1">忍者狂刀</string>
<string name="app_choice2">最后一战:星河战役</string>
<string name="app_choice3">吉他英雄:世界轮回乐队</string>
</resources>
my_anim.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="0.1"
android:toAlpha="1.0"
android:duration="3000"
/>
<scale
android:interpolator= "@android:anim/accelerate_decelerate_interpolator"
android:fromXScale = "0.0"
android:toXScale = "1.4"
android:fromYScale = "0.0"
android:toYScale = "1.4"
android:pivotX = "50%"
android:pivotY = "50%"
android:fillAfter = "false"
android:duration = "7000"
/>
<translate
android:fromXDelta = "30"
android:toXDelta = "-80"
android:fromYDelta = "30"
android:toYDelta = "300"
android:duration = "5000"
/>
<rotate
android:interpolator= "@android:anim/accelerate_decelerate_interpolator"
android:fromDegrees = "0"
android:toDegrees = "+350"
android:pivotX = "50%"
android:pivotY = "50%"
android:duration = "3000"
/>
</set>