鼓励自己,每学一个技术就写一篇博客。
这是自己在csdn的第一篇博客。记录下。
Dialog是自己接触android学习的第一个控件,当初还是看《雨松MOMO安卓博客课程》学习的。转眼4个月已经过去了。最近看了好多大神的文章都鼓励要写博客,所以就来参与了。虽然之前在ACM打酱油的时候也写过。
毕竟是4个月前的代码了,所以风格跟现在的也有很大差距了,再加上代码页差不多忘了,最近又公司任务多,而且这个例子也没有什么难度了,就不多做介绍了。
该例子可能对初学者有用,各种牛人就把这给忽略掉吧。
以下是效果图:
首先当然是先写一个layout了,以下是layout的部分代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:id="@+id/wenzi"
android:text="@string/hello_world" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@id/wenzi"
android:text="@string/san_btn"
android:id="@+id/btn1"
/>
接下去是自定义对话框的layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:id="@+id/dialogname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/tvUserName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:text="姓名:" />
<EditText
android:id="@+id/etUserName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="200dip" />
</LinearLayout>
<LinearLayout
android:id="@+id/dialognum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/dialogname"
android:orientation="horizontal" >
<TextView
android:id="@+id/tvPassWord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:text="密码:" />
<EditText
android:id="@+id/etPassWord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="200dip" />
</LinearLayout>
</RelativeLayout>
然后是activity.java。这里紧截取部分由意义的代码。
public void CreateDialog(int id){
AlertDialog.Builder builder= new AlertDialog.Builder(MainActivity.this);
switch(id){
case DIALOG_1:
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("你是男的女的?");
builder.setPositiveButton("男", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
showDialog("您的性别是男性");
}
});
builder.setNegativeButton("女", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
showDialog("您的性别是女性");
}
});
break;
case DIALOG_2:
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("你是男的女的?");
builder.setPositiveButton("男", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
showDialog("您的性别是男性");
}
});
builder.setNeutralButton("你猜", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
showDialog("您的性别是你猜");
}
});
builder.setNegativeButton("女", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
showDialog("您的性别是女性");
}
});
break;
case DIALOG_3:
mChickNum=0;
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("单选框,选择一个你喜欢的");
builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mChickNum=which;
showDialog("您喜欢的是:"+items[mChickNum]+" 他的id是:"+mChickNum);
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
showDialog("您点击了确定,您喜欢的是:"+items[mChickNum]+" 他的id是:"+mChickNum);
}
});
builder.setNegativeButton("离开", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
break;
case DIALOG_4:
mCheckNum.clear();
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("多选框,选择一些你喜欢的");
builder.setMultiChoiceItems(items,
new boolean[] {false,false,false,false,false,false,false},
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
mCheckNum.add(which);
showDialog("您喜欢的是:"+items[which]+" 他的id是:"+which);
}else{
mCheckNum.remove(which);
}
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String strCheck="";
int mSize=mCheckNum.size();
for(int i=0;i<mSize;i++){
strCheck+=items[mCheckNum.get(i)];
}
showDialog("您点击了确定,您喜欢的是:"+strCheck);
}
});
builder.setNegativeButton("离开", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
break;
case DIALOG_5:
builder.setTitle("类表框显示");
builder.setIcon(R.drawable.ic_launcher);
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
showDialog("您选择的是"+items[which]);
}
});
break;
case DIALOG_6:
mprogreedialog=new ProgressDialog(MainActivity.this);
mprogreedialog.setIcon(R.drawable.ic_launcher);
mprogreedialog.setTitle("进度条演示");
mprogreedialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mprogreedialog.setMax(MAX_DIALOG);
mprogreedialog.setButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
showDialog("当前进度为:"+mprogreedialog.getProgress());
}
});
mprogreedialog.show();
new Thread(this).start();
return;
case DIALOG_7:
mprogreedialog=new ProgressDialog(MainActivity.this);
mprogreedialog.setIcon(R.drawable.ic_launcher);
mprogreedialog.setTitle("读取进度条演示");
mprogreedialog.setMessage("正在读取中。。。");
mprogreedialog.setIndeterminate(true);
mprogreedialog.setCancelable(true);
mprogreedialog.show();
return;
case DIALOG_8:
LayoutInflater factory=LayoutInflater.from(this);
final View textEntryView=factory.inflate(R.layout.test, null);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("自定义对话框");
builder.setView(textEntryView);
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
EditText userName= (EditText) textEntryView.findViewById(R.id.etUserName);
EditText userPsw=(EditText) textEntryView.findViewById(R.id.etPassWord);
showDialog("用户名为:"+userName.getText().toString()+" 密码为:"+userPsw.getText().toString());
}
});
break;
}
builder.create().show();
}
public void showDialog(String str){
new AlertDialog.Builder(MainActivity.this).setMessage(str).show();
}
由于代码较为简单,这里就不多介绍了。大家看着就懂了。
有兴趣可以去下载我的源码。http://download.csdn.net/detail/han123456o/6022051
个人觉得初学者用《雨松MOMO安卓博客课程》还是很有用的,这里推荐下了。