一个按钮做大选择
来源:互联网 发布:linux停止进程命令 编辑:程序博客网 时间:2024/04/29 12:02
首先在value/string.xml文件中:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">波特率选择</string>
<string name="app_name">选择</string>
<!-- 选择的内容 -->
<array name="baud_rate">
<item>B9600</item>
<item>B57600</item>
<item>B115200</item>
</array>
</resources>
在layout/main.xml中,有一个textview和button:
<TextView
android:id="@+id/myTextView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:text="波特率选择" />
<Button
android:id="@+id/myButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="100dip"
android:layout_y="30dip"
android:text="选择波特率" />
当我们按下这个按钮后,出现选择对话框,具体java代码实现如下:
public class button extends Activity {
public Button mButton1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mButton1 =(Button) findViewById(R.id.myButton1);
//设置按钮监听对象
mButton1.setOnClickListener(myShowAlertDialog);
}
//监听按钮按下并在按下做相应处理
Button.OnClickListener myShowAlertDialog = new Button.OnClickListener()
{
public void onClick(View arg0) {
new AlertDialog.Builder(button.this)
//设置标题
.setTitle("baudrate")
//设置选择数组和监听函数
.setItems(R.array.baud_rate, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichcountry) {
CharSequence strDialogBody = "你选择的是:";
//标签数组
String[] aryShop = getResources().getStringArray(R.array.baud_rate);
/************************选中标签后出现的对话框*****************/
new AlertDialog.Builder(button.this)
//显示 "你选择的是:"+选择的标签
.setMessage(strDialogBody + aryShop[whichcountry])
/*一般有三个Button:PositiveButton,NegativeButton,NeutralButton。
* 从名字可以看的出来,代表确定,否定,和中立,其实三个Button可以
* 写你任意的方法,只是位置上的不同而已,确定Button一般靠左,这是
* 阅读习惯。本质上都是三个Button并没有很大的区别*/
.setNeutralButton("确认", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/*在这里处理要作的事*/
}
}
)
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
}
).show();
/********************选中标签后的对话框到此结束*******************/
}
})
//主对话框只有一个取消键
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface d, int which) {
d.dismiss();
}
}
) .show();
}
};
}
- 一个按钮做大选择
- javascript做按钮日期选择
- 选择屏幕上做按钮
- 一个css做的按钮
- 如何在JSP页面中通过一个按钮,在对话框中选择一个目录而不是选择一个文件;input type=file只能选择一个文件,我想选择一个目录;做不出来项目经理要扣奖金的...............
- 一个数据挖掘大牛,用程序算法做人生选择【转载】
- 自己做的一个JAVA swing按钮
- 继承JButton,做一个圆形的按钮。
- 如何做一个按钮二个事件
- WPF做一个闪动的按钮
- 用SDL做一个退出按钮
- 我不会选择做一个普通人的
- 选择按钮
- 选择按钮
- 公司做网站选择建站的几大原则
- 点击按钮进入一个只能选择的页面
- RadioButtonList当选择一个按钮时textbox变为只读
- 【MFC】按钮选择一个文件夹,获得文件夹中文件路径!
- 解决ViewPager中Gallery无法滑动问题
- mysqldump参数详细说明
- 向上级团委汇报工作
- 一个windows开源socket 库
- BlockingQueue详解
- 一个按钮做大选择
- 几大基本排序算法原理
- java.lang.OutOfMemoryError: PermGen space
- Android中Bitmap,byte[],Drawable相互转化
- 老张喝茶 教你同步异步 阻塞与非阻塞
- 第一篇blog,有一篇就有第二篇,就有更多。。。
- List类型与String数组、Set集合互相转换
- exists\like\in\=效率比较
- 在Windows中的安装Android NDK开发环境