安卓课程二十五 各种对话框
这节学习安卓的各种对话框。包含
1)一个确认按键的对话框
2)一个确认键和取消键的对话框
3)包括三个按键的对话框
4)下拉列表对话框
5)单选下拉列表对话框
6)多选下拉列表对话框
7)进度条对话框
8)进度条提示对话框
9)自定义对话框
效果如下:
将代码贴出来,便于分析。
strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Dialog</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <string name="alert_one_button">确定对话框</string> <string name="alert_button">确定取消对话框</string> <string name="more_button">多个按钮信息对话框</string> <string name="select_button">列表选项卡</string> <string name="select_radio_button">单选列表对话框选项卡</string> <string name="progress_button">进度条对话框按钮</string> <string name="checkbox_button">多选对话框按钮</string> <string name="diy_button">自定义对话框按钮</string> <string name="progressing_button">读取进度按钮</string> <string name="diy_uname">姓名</string> <string name="diy_pwd">密码</string> </resources>
activity_main.xml
<LinearLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical"
>
<Button android:id="@+id/alert_one_btn"
android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/alert_one_button"/>
<Button android:id="@+id/alert_btn"
android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/alert_button"/>
<Button android:id="@+id/more_btn"
android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/more_button"/>
<Button android:id="@+id/select_btn"
android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/select_button"/>
<Button android:id="@+id/radio_select_btn"
android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/select_radio_button"/>
<Button android:id="@+id/progress_btn"
android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/progress_button"/>
<Button android:id="@+id/checkbox_btn"
android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/checkbox_button"/>
<Button android:id="@+id/diy_btn"
android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/diy_button"/>
<Button android:id="@+id/progressing_btn"
android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/progressing_button"/>
</LinearLayout>
MainActivity.java
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener,Runnable {
/**
* 记录进度条中进度信息
*/
private int MAX_PROGRESS = 10;
private ProgressDialog mProgressDialog;
/**
* 记录多选框对话框中选中的信息
*/
private ArrayList <Integer>MultiChoiceID = new ArrayList <Integer>();
private final String[] mItems = {"item0","item1","itme2","item3","itme4","item5","item6"};
//mSingleChoice 用于记录单选中的ID
int mSingleChoiceID = -1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/**
* 只有一个确认建
*/
Button alert_one_button = (Button) this.findViewById(R.id.alert_one_btn) ;
alert_one_button.setOnClickListener(this);
/**
* 有两个键,确认和取消
*/
Button alert_btn = (Button) this.findViewById(R.id.alert_btn) ;
alert_btn.setOnClickListener(this);
/**
* 三个按键的
*/
Button more_btn = (Button) this.findViewById(R.id.more_btn);
more_btn.setOnClickListener(this);
/**
* 下拉选的
*/
Button select_btn = (Button) this.findViewById(R.id.select_btn) ;
select_btn.setOnClickListener(this) ;
/**
* 下拉单选提示框
*/
Button radio_select_btn = (Button) this.findViewById(R.id.radio_select_btn) ;
radio_select_btn.setOnClickListener(this) ;
/**
* 进度条按钮
*/
Button progress_btn = (Button) this.findViewById(R.id.progress_btn);
progress_btn.setOnClickListener(this) ;
/**
* 多选列表按钮
*/
Button checkbox_button = (Button)this.findViewById(R.id.checkbox_btn);
checkbox_button.setOnClickListener(this) ;
/**
* 自定义对话框
*/
Button diy_btn = (Button)this.findViewById(R.id.diy_btn);
diy_btn.setOnClickListener(this) ;
/**
* 进度条选的
*/
Button progressing_btn=(Button)this.findViewById(R.id.progressing_btn);
progressing_btn.setOnClickListener(this) ;
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void showDialog(String str) {
Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG).show();
}
public void onClick(View v) {
switch(v.getId()) {
case R.id.alert_one_btn:
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.drawable.logo3);
builder.setTitle("我们向您发送通知呢");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//这里添加点击确定后的逻辑
showDialog("你选择了确定");
}
});
builder.create().show();
break;
case R.id.alert_btn:
AlertDialog.Builder builder1 = new AlertDialog.Builder(MainActivity.this);
builder1.setIcon(R.drawable.logo3);
builder1.setTitle("你确定要离开吗?");
builder1.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//这里添加点击确定后的逻辑
showDialog("你选择了确定");
}
});
builder1.setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//这里添加点击取消后的逻辑
showDialog("你选择了取消");
}
});
builder1.create().show();
break;
case R.id.more_btn:
AlertDialog.Builder builder2 = new AlertDialog.Builder(MainActivity.this);
builder2.setIcon(R.drawable.logo3);
builder2.setTitle("投票");
builder2.setMessage("您认为什么样的内容能吸引您?");
builder2.setPositiveButton("有趣味的", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
showDialog("你选择了有趣味的");
}
});
builder2.setNeutralButton("有思想的", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
showDialog("你选择了有思想的");
}
});
builder2.setNegativeButton("主题强的", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
showDialog("你选择了主题强的");
}
});
builder2.create().show();
break;
case R.id.select_btn:
AlertDialog.Builder builder3 = new AlertDialog.Builder(MainActivity.this);
builder3.setTitle("列表选择框");
builder3.setItems(mItems, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//点击后弹出窗口选择了第几项
showDialog("你选择的id为" + which + " , " + mItems[which]);
}
});
builder3.create().show();
break;
case R.id.radio_select_btn:
AlertDialog.Builder builder4 = new AlertDialog.Builder(MainActivity.this);
builder4.setIcon(R.drawable.logo3);
builder4.setTitle("单项选择");
builder4.setSingleChoiceItems(mItems, 0, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
mSingleChoiceID = whichButton;
showDialog("你选择的id为" + whichButton + " , " + mItems[whichButton]);
}
});
builder4.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
showDialog("whichButton="+whichButton);
if(mSingleChoiceID >= 0) {
showDialog("你选择的是" + mSingleChoiceID);
}
}
});
builder4.setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
builder4.create().show();
break;
case R.id.progress_btn:
mProgressDialog = new ProgressDialog(MainActivity.this);
mProgressDialog.setIcon(R.drawable.logo3);
mProgressDialog.setTitle("进度条窗口");
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setMax(MAX_PROGRESS);
mProgressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//这里添加点击后的逻辑
showDialog("你选择的是确定" );
}
});
mProgressDialog.setButton(DialogInterface.BUTTON_NEGATIVE,"取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//这里添加点击后的逻辑
showDialog("你选择的是取消" );
}
});
mProgressDialog.show();
new Thread(this).start();
break;
case R.id.checkbox_btn:
AlertDialog.Builder builder5 = new AlertDialog.Builder(MainActivity.this);
MultiChoiceID.clear();
builder5.setIcon(R.drawable.logo3);
builder5.setTitle("多项选择");
builder5.setMultiChoiceItems(mItems,
new boolean[]{false, false, false, false, false, false, false},
new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog, int whichButton,
boolean isChecked) {
if(isChecked) {
MultiChoiceID.add(whichButton);
showDialog("你选择的id为" + whichButton + " , " + mItems[whichButton]);
}else {
MultiChoiceID.remove(whichButton);
}
}
});
builder5.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String str = "";
int size = MultiChoiceID.size();
for (int i = 0 ;i < size; i++) {
str+= mItems[MultiChoiceID.get(i)] + ", ";
}
showDialog("你选择的是" + str);
}
});
builder5.setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
builder5.create().show();
break;
case R.id.diy_btn:
AlertDialog.Builder builder6 = new AlertDialog.Builder(MainActivity.this);
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(R.layout.diydialog, null);
builder6.setIcon(R.drawable.logo3);
builder6.setTitle("自定义输入框");
builder6.setView(textEntryView);
builder6.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
EditText userName = (EditText) textEntryView.findViewById(R.id.etUserName);
EditText password = (EditText) textEntryView.findViewById(R.id.etPassWord);
showDialog("姓名 :" + userName.getText().toString() + "密码:" + password.getText().toString() );
}
});
builder6.setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
builder6.create().show();
break;
case R.id.progressing_btn:
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setIcon(R.drawable.logo3);
mProgressDialog.setTitle("读取ing");
mProgressDialog.setMessage("正在读取中请稍候");
mProgressDialog.setIndeterminate(true);
mProgressDialog.setCancelable(true);
mProgressDialog.show();
//mProgressDialog.cancel();
break;
}
}
public void run() {
int Progress = 0;
while(Progress < MAX_PROGRESS) {
try {
Thread.sleep(100);
Progress++;
mProgressDialog.incrementProgressBy(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
diydialog.xml
<?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:textSize="20sp" android:text="@string/diy_uname" /> <EditText android:id="@+id/etUserName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="text" 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:textSize="20sp" android:text="@string/diy_pwd" /> <EditText android:id="@+id/etPassWord" android:inputType="textWebPassword" android:layout_width="wrap_content" android:layout_height="wrap_content" android:minWidth="200dip" /> </LinearLayout> </RelativeLayout>
讲到自定义布局我就得多说一说了,为什么要多说一说呢?
其实自定义布局在Android的开发中非常重要 因为它能让开发者做出自己五彩缤纷的Activity 而不用去使用系统枯燥的界面。
其实自定义布局在Android的开发中非常重要 因为它能让开发者做出自己五彩缤纷的Activity 而不用去使用系统枯燥的界面。
自定义dialog有什么好处?
比如我们在开发过长当中 要通过介绍系统发送的一个广播弹出一个dialog . 但是dialog必需是基于activity才能呈现出来 如果没有activity 的话 程序就会崩溃。所以我们可以写一个自定义的 dialog 把它定义成一个activity
这样我们收到一条打开dialog的广播后 直接启动这个 activity 程序正常运行~~
这就是自定义dialog的好处。
注明:例子中只是写了自定义dialog 没有把它单独的写在一个activity中