Android Dialog用法

Android Dialog用法
[url]http://blog.csdn.net/ameyume/article/details/6138488[/url]

玩转Android---UI篇---Dialog(对话框):
[url]http://hualang.iteye.com/blog/968245[/url]

android自定义登陆Dialog [url]http://www.cnblogs.com/xuhaiqing/archive/2011/09/30/2196576.html[/url]


Android API 支持下列类型的对话框对象:
  警告对话框 AlertDialog: 一个可以有0到3个按钮, 一个单选框或复选框的列表的对话框. 警告对话框可以创建大多数的交互界面, 是推荐的类型.
  进度对话框 ProgressDialog: 显示一个进度环或者一个进度条. 由于它是AlertDialog的扩展, 所以它也支持按钮.
  日期选择对话框 DatePickerDialog: 让用户选择一个日期.
  时间选择对话框 TimePickerDialog: 让用户选择一个时间.
  如果你希望自定义你的对话框, 可以扩展Dialog类.
/mnt/d/android/idea/ADemo4/res/values/array.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="select_dialog_items">
<item>Command one</item>
<item>Command two</item>
<item>Command three</item>
<item>Command four</item>
</string-array>
</resources>



[b]1.AlertDialog[/b]
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mButton = (Button) findViewById(R.id.button);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
(new AlertDialog.Builder(MyActivity.this)).setMessage("确定是否退出程序")
.setCancelable(false)
.setPositiveButton("确定退出", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//To change body of implemented methods use File | Settings | File Templates.
MyActivity.this.finish();
}
})
.setNegativeButton("取消推出",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
})
.create()
.show();
}
});
}


[b]2.ListDialog[/b] setMessage()和setItems()不能同时设定,否则之显示一种。
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
(new AlertDialog.Builder(MyActivity.this))
.setTitle("对话框头部")
//.setMessage("对话框的附加信息")
.setItems(R.array.select_dialog_items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which) {
String[] items =
getResources().getStringArray(R.array.select_dialog_items);
new AlertDialog.Builder(MyActivity.this)
.setMessage("You selected: " + which + " , " + items[which])
.show();

}
}).create()
.show();
}
});


[b]3.RadioDialog[/b]
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
(new AlertDialog.Builder(MyActivity.this))
.setTitle("对话框头部")
.setSingleChoiceItems(R.array.select_dialog_items, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which) {
String[] items =
getResources().getStringArray(R.array.select_dialog_items);
new AlertDialog.Builder(MyActivity.this)
.setMessage("You selected: " + which + " , " + items[which])
.show();

}
}).create()
.show();
}
});


[b]4.CheckDialog[/b] 使用OnMultiChoiceClickListener
Button button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
(new AlertDialog.Builder(MyActivity.this))
.setTitle("对话框头部")
.setMultiChoiceItems(R.array.select_dialog_items,
new boolean[]{false, true, false, true},
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i, boolean b) {
Toast toast = Toast.makeText(MyActivity.this,i+":"+b, Toast.LENGTH_SHORT);
toast.show();
}
})
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
})

.create()
.show();
}
});


[b]5.自定义布局对话框[/b]
/mnt/d/android/idea/ADemo4/res/layout/dialog.xml 布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New EditText"
android:id="@+id/editText" android:layout_gravity="left|center_vertical"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New EditText"
android:id="@+id/editText1" android:layout_gravity="left|center_vertical"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button" android:layout_gravity="left|center_vertical"/>
</LinearLayout>

        LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(R.layout.dialog, null);//创建布局

Button button4 = (Button) findViewById(R.id.button4);
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
(new AlertDialog.Builder(MyActivity.this))
.setTitle("对话框头部")
.setView(textEntryView)//使用布局
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
})

.create()
.show();
}
});



.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值