Android小项目之---选择对话框(附源码)

还记得早先我们做的记算器的例子吗?当中的验证判断用到了对话框,今天我们来做一个不一样的对话框,要做的这个小例子是一个可供选择效果的对话框即层层迭迭的Alert Dialog;界面方面我们摆放一个Button来做一个按钮事件,解发这个按钮事件后,再通过类似列表项目的方式呈现在Alert Dialog 里面。
功能用途:投票、选择器、遥控器等类型。。。最后返回程序,取得用户选择菜单项目结果。

效果图:

 

     

 

程序中用到的数据资源如下:

 

复制代码
<resources>
 
    <string name="hello">K..L.. 好餓呀,要上哪吃飯</string>
 
    <string name="app_name">有選擇的對話框</string>[color=#333333][font=Georgia]
 
<string name="Choose">按我開始</string>
 
<string name="Tittle">按我開始選擇</string>
 
<string name="body">你選選擇的是:</string>
 
<string name="Ok">確定</string> 
<string-array name="Dialog">
 
    <item>韓國餐廳</item>
 
    <item>一站式餐廳</item>
 
    <item>回家湘</item>
 
    <item>富州酒店</item>
 
</string-array>
 
</resources>
复制代码

功能代码如下:因为使用了选择功能的对话框,可能初期看代码有点复杂,建议利用eclipse的文档大纲先把代码层次分好,有利用观看和理解代码。

 

复制代码
import android.app.Activity; [indent] 
import android.app.AlertDialog;
 
import android.content.DialogInterface;
 
import android.os.Bundle;
 
import android.view.View;
 
import android.view.View.OnClickListener;
 
import android.widget.*;
 

public class ChosseDialog extends Activity {
 

private Button mButton;
 
private TextView mTextView; 
private CharSequence Text;
 

/** Called when the activity is first created. */
 
public void onCreate(Bundle savedInstanceState) {
 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main);
 
mButton=(Button)findViewById(R.id.Button01);
 
mTextView=(TextView)findViewById(R.id.TextView01);
 
mButton.setText(R.string.Choose);
 
mTextView.setText("請選擇");
 
mButton.setOnClickListener(new OnClickListener() {
 

@Override
 
public void onClick(View v) {
 
// TODO Auto-generated method stub
 
new AlertDialog.Builder(ChosseDialog.this).setTitle(R.string.hello)
 
.setItems(R.array.Dialog, new DialogInterface.OnClickListener() {
 
@Override
 
public void onClick(DialogInterface dialog, int which) {
 
// TODO Auto-generated method stub
 

CharSequence strDialog=getString(R.string.body);
 
String[] aryShop=getResources().getStringArray(R.array.Dialog); 
Text=strDialog+aryShop[which];
 
new AlertDialog.Builder(ChosseDialog.this)[/indent] .setTitle(R.string.Tittle)
 
.setMessage(Text).setNegativeButton(R.string.Ok, new DialogInterface.OnClickListener() {
 
@Override
 
public void onClick(DialogInterface dialog, int which) {
 

// TODO Auto-generated method stub
 
mTextView.setText(Text);
 
}
 
}).show();
 
}
 
}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
 
@Override
 
public void onClick(DialogInterface dialog, int which) {
 
// TODO Auto-generated method stub
 
dialog.dismiss();
 
}
 
}).show();
 
}
 
});
 
}
 
复制代码

 

 

 

 

 

 

posted on   vus520  阅读(1078)  评论(0编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架

导航

< 2011年12月 >
27 28 29 30 1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
1 2 3 4 5 6 7
点击右上角即可分享
微信分享提示