对话框应用进阶
4.25 猜猜我在想什么
范例程序先设计一个RadioGroup,并在当中配置3个RadioButton,每一个RadioButton各自有其ID(通过getId()方法),并预先将同一个Group里的RadioButton选项里的ID存入整数数组中,当User触发RadioGroup的OnCheckedChangeListener()事件的同时,比较答案是否正确。本范例的学习重点在于访问RadioButton ID的技巧,并使之属于同一个RadioGroup(整数ID),常应用于单选题投票、不同RadioGroup菜单等。
范例程序
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
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.TextView;
public class ex4_25 extends Activity {
protected Button mButton1,mButton2;
protected TextView mTextView1;
protected RadioGroup mRadioGroup1;
protected boolean mUserChoice = false;
protected int mMyChoice = -2;
protected int intTimes = 0;
protected RadioButton mRadio1,mRadio2,mRadio3;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mButton1 =(Button) findViewById(R.id.myButton1);
mButton2 =(Button) findViewById(R.id.myButton2);
mTextView1 = (TextView) findViewById(R.id.myTextView1);
/* RadioGroup Widget */
mRadioGroup1 = (RadioGroup) findViewById(R.id.myRadioGroup1);
mRadio1 = (RadioButton) findViewById(R.id.myOption1);
mRadio2 = (RadioButton) findViewById(R.id.myOption2);
mRadio3 = (RadioButton) findViewById(R.id.myOption3);
/* 取得三个RadioButton的ID,并存放置整数数组中 */
int[] aryChoose = {mRadio1.getId(), mRadio2.getId(), mRadio3.getId()};
/* 以随机数的方式指定哪一个为系统猜测的答案 */
int intRandom = (int) (Math.random() * 3);
mMyChoice = aryChoose[intRandom];
/* 回答按钮事件 */
mButton1.setOnClickListener(mChoose);
/* 清空按钮事件 */
mButton2.setOnClickListener(mClear);
/* RadioGruop当User更改选项后的事件处理 */
mRadioGroup1.setOnCheckedChangeListener(mChangeRadio);
}
/* RadioGruop当User更改选项后的事件处理 */
private RadioGroup.OnCheckedChangeListener mChangeRadio = new RadioGroup.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
// TODO Auto-generated method stub
if(checkedId==mMyChoice)
{
/* User猜对了*/
mUserChoice = true;
}
else
{
/* User猜错了*/
mUserChoice = false;
}
}
};
/* 回答按钮事件 */
private Button.OnClickListener mChoose = new Button.OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
if(mUserChoice)
{
mTextView1.setText(R.string.str_guess_correct);
mUserChoice = false;
intTimes = 0;
mRadio1 = (RadioButton) findViewById(R.id.myOption1);
mRadio2 = (RadioButton) findViewById(R.id.myOption2);
mRadio3 = (RadioButton) findViewById(R.id.myOption3);
int[] aryChoose = {mRadio1.getId(), mRadio2.getId(), mRadio3.getId()};
int intRandom = (int) (Math.random() * 3);
mMyChoice = aryChoose[intRandom];
mRadioGroup1.clearCheck();
}
else
{
intTimes++;
mTextView1.setText(getResources().getString(R.string.str_guess_error)+"("+Integer.toString(intTimes)+")");
Animation shake = AnimationUtils.loadAnimation(ex4_25.this, R.anim.shake);
v.startAnimation(shake);
}
}
};
/* 清空按钮事件 */
private Button.OnClickListener mClear = new Button.OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
mUserChoice = false;
intTimes = 0;
mRadio1 = (RadioButton) findViewById(R.id.myOption1);
mRadio2 = (RadioButton) findViewById(R.id.myOption2);
mRadio3 = (RadioButton) findViewById(R.id.myOption3);
int[] aryChoose = {mRadio1.getId(), mRadio2.getId(), mRadio3.getId()};
int intRandom = (int) (Math.random() * 3);
mMyChoice = aryChoose[intRandom];
mTextView1.setText(R.string.hello);
mRadioGroup1.clearCheck();
}
};
}
拓展应用
RadioGroup可动态配置其Layout,方法如下:
LinearLayout.LayoutParams
lp = new RadioGroup.LayoutParams
(
RadioGroup.LayoutParams.WRAP_CONTENT,
RadioGroup.LayoutParams.WRAP_CONTENT
);
若欲利用程序动态创建RadioGroup以及RadioButton,方法如下:
/* 动态创建RadioGroup */
RadioGroup mRadioGroup01 = new RadioGroup(this);
RadioGroup.LayoutParams layoutParams = new RadioGroup.LayoutParams
(
RadioGroup.LayoutParams.WRAP_CONTENT,
RadioGroup.LayoutParams.WRAP_CONTENT
);
mRadioGroup01.setLayoutParams(layoutParams);
this.addContentView(mRadioGroup01, layoutParams);
/* 动态创建RadioButton 1 */
RadioButton mRadioButton01 = new RadioButton(this);
mRadioButton01.setText("选项1");
RadioGroup.LayoutParams layout1 = new RadioGroup.LayoutParams
(
RadioGroup.LayoutParams.WRAP_CONTENT,
RadioGroup.LayoutParams.WRAP_CONTENT
);
mRadioGroup01.addView(mRadioButton01, 0, layout1);
/* 动态创建RadioButton 2 */
RadioButton mRadioButton02 = new RadioButton(this);
mRadioButton02.setText("选项2");
RadioGroup.LayoutParams layout2 = new RadioGroup.LayoutParams
(
RadioGroup.LayoutParams.WRAP_CONTENT,
RadioGroup.LayoutParams.WRAP_CONTENT
);
mRadioGroup01.addView(mRadioButton02, 1, layout2);
最后要介绍的是当User选择错误时,也可以在Button.OnClickListener里判断后,显示Animation,摇晃按钮,只要编写类似如下的代码段即可。
private Button.OnClickListener mChoose = new Button.OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
if(mUserChoice)
{
/* User答对了,程序略 */
}
else
{
Animation shake = AnimationUtils.loadAnimation
(EX04_25.this, R.anim.shake);
v.startAnimation(shake);
}
}
};
|
此范例将说明设置离开与关闭程序的对话窗口提示选项。而打开这个对话窗口是由一个按钮来处理,当单击“按我离开”按钮时,会出现AlertDialog的信息,而AlertDialog的内容设置,先设置是否要显示title的文字(也可以不设置),该范例是会让它显示的,所以使用res/values/ string/app_about这个字符串常数。
下一步则是显示AlertDialog的正文信息,使用的方法是.setMessage,如果不使用的话,就会发现AlertDialog的正文的字段是不显示的,故在这边设置显示内容为提示对话内容,正文的信息则是res/values/string/app_msg。
而本范例最重要的并非是AlertDialog的设计或流程,因为这些在前一章已经看过了,在此要示范的是如何在AlertDialog里显示图片、配置“确定”以及“取消”按钮,并学会如何判断在AlertDialog里单击的按钮事件,即设计在离开程序之前跳出AlertDialog,让User再一次确认是否关闭应用程序,并在AlertDialog上设计“确定”与“取消”按钮,当单击“确定”按钮才会真的离开程序,单击“取消”按钮则会回到程序中。
主程序设置的AlertDialog有两个Button,或许有人会询问:Button没有在layout/main.xml里创建,为何会出现呢?这是因为在动态生成AlertDialog的同时,也重写了按钮事件;AlertDialog里面的Button语句,与一般在Layout当中先部署Button不一样,“确定”Button跟“取消”Button都会有追加方法的描述。如“确定”Button的描述是PositiveButton,而“取消”Button的命令为NegativeButton。
在构造AlertDialog内的“确定”Button中的OnClickListener事件后,如果发生Positive Button事件,则以finish()结束程序,程序就会自行关闭,这个finish()代表的意思,是把整个Activity给关掉,并由Android.os回收已配置的系统资源。
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class ex4_26 extends Activity {
/*声明 Button对象*/
private Button mButton1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/*取得 Button对象*/
mButton1 = (Button) findViewById(R.id.myButton1);
mButton1.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
new AlertDialog.Builder(ex4_26.this)
/*弹出窗口的最上头文字*/
.setTitle(R.string.app_about)
/*设置弹出窗口的图式*/
.setIcon(R.drawable.hot)
/*设置弹出窗口的信息*/
.setMessage(R.string.app_about_msg)
.setPositiveButton(R.string.str_ok,
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialoginterface, int i)
{
finish();/*关闭窗口*/
}
}
)
/*设置跳出窗口的返回事件*/
.setNegativeButton(R.string.str_no,
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialoginterface, int i)
{
}
})
.show();
}
});
}
}
扩展学习
AlertDialog.setIcon()可以置换对话窗口里的图标,在不设置此方法时就只有文字,若使用了setIcon()方法后,就会将图标显示在对话窗口当中。
你可以试着使用setIcon()方法将Drawable ID传入(默认的drawable路径为res/drawable)即可,以下的代码段可将原本的感叹号图标更换为一个心碎的小图标。
new AlertDialog.Builder(EX04_26.this)
/*弹出窗口的最上头文字*/
.setTitle(R.string.app_about)
/*设置弹出窗口的图式*/
.setIcon(R.drawable.hot)
/*设置弹出窗口的信息*/
//.setMessage(R.string.app_about_msg)
需留意放入的Icon图片不可以太大,最好先缩图之后再导入项目,若能参考手机画面的分辨率来设计更佳。