singleSelectDialog...单选按钮,有确定按钮

------------------------main.java------------------------


package com.example.mm;


import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
final int SINGLE_DIALOG = 0x113;
@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bn = (Button)findViewById(R.id.bn);
//为按钮绑定事件监听器
bn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View source)
{
//显示对话框
showDialog(SINGLE_DIALOG);
}
});
}
//重写Activity里面的方法
@Override
@Deprecated
protected Dialog onCreateDialog(int id, Bundle args) {
//判断需要生成哪种类型的对话框
switch (id)
{
case SINGLE_DIALOG:
Builder b = new AlertDialog.Builder(this);
// 设置对话框的图标
b.setIcon(R.drawable.icon);
// 设置对话框的标题
b.setTitle("单选列表对话框");
// 为对话框设置多个列表
b.setSingleChoiceItems(new String[]
{ "红色", "绿色", "蓝色" }
// 默认选中第二项
, 1
//为列表项的单击事件设置监听器
, new OnClickListener()
{
@Override
public void onClick(DialogInterface dialog,
int which)
{
TextView show = (TextView) findViewById(R.id.show);
// which代表哪个列表项被单击了
switch (which)
{
//修改文本框的背景色
case 0:
show.setBackgroundColor(Color.RED);
break;
case 1:
show.setBackgroundColor(Color.GREEN);
break;
case 2:
show.setBackgroundColor(Color.BLUE);
break;
}
}
});
// 添加一个“确定”按钮,用于关闭该对话框
b.setPositiveButton("确定", null);
// 创建对话框
return b.create();
}
return null;
}



}


--------------------------------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"
android:gravity="center_horizontal"
>
<TextView
android:id="@+id/show" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="测试文本内容"
android:textSize="11pt"
android:background="#ff00ff00"
/>
<Button 
android:id="@+id/bn" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="选择颜色"
/>
</LinearLayout>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值