android常见的弹窗对话框

来源:互联网 发布:大数据认证考试 编辑:程序博客网 时间:2024/06/05 05:43

一个布局文件一个activity


package com.alleged.Alert;


import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.os.Bundle;
import android.widget.Button;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity{
private Context context;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
// 从activity中找到按钮
/* Button normalDialog = (Button) findViewById(R.id.normalDialog);
Button MultipleChoice = (Button) findViewById(R.id.MultipleChoice);
Button multi_select = (Button) findViewById(R.id.multi_select);
Button progressSeclect = (Button) findViewById(R.id.progressSeclect);*/
// 给各个按钮设置点击事件
}


public void progressSeclect(View v) {
ProgressDialog progressdialog = new ProgressDialog(context);
progressdialog.setTitle("进度条对话框");
progressdialog.setMessage("狂奔吧小蜗牛");
progressdialog.setIndeterminate(false);
progressdialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressdialog.setMax(100000);
for(int i=1;i<=100000;i++){
progressdialog.incrementProgressBy(i);
progressdialog.incrementSecondaryProgressBy(i+10);


}
progressdialog.show();
}


private void sleep(int i) {
// TODO Auto-generated method stub

}


public void multiselect(View v) {
// TODO Auto-generated method stub
//初始化布尔数组,默认选中
final boolean[] checkedItems = new boolean[] { true, false, false, false, false };
final String[] dream = new String[]{"美满家庭","亿万富翁","诗人","撩妹小王子","家"};
AlertDialog.Builder builder = new Builder(context);
builder.setTitle("请选择你想要");
builder.setMultiChoiceItems(dream, checkedItems, new OnMultiChoiceClickListener() {

@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
// 更改boolean数组 表示是否选中
checkedItems[which] = isChecked;
}
});
builder.setPositiveButton("确定", new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// 因为是复选 所以这里要遍历循环输出
StringBuffer sb = new StringBuffer();
for(int i=0;i<checkedItems.length;i++){
if(checkedItems[i]){
sb.append(dream[i]+" ");
}
}
Toast.makeText(context, sb, 0).show();

}
});
builder.show();
}


public void Multiplechoice(View v) {

final String[] dream = new String[]{"美满家庭","亿万富翁","诗人","撩妹小王子","家"};
//创建出builder对象
AlertDialog.Builder builder = new Builder(context);
//设置标题
builder.setTitle("请选择你想要");
//设置单选对象
builder.setItems(dream, new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
//which选择的下标
dialog.dismiss();
Toast.makeText(context, dream[which], 0).show();
}
});
//弹窗展示
builder.show();


}


public void normaldialog(View v) {
//弹出普通的对话窗
AlertDialog.Builder builder = new Builder(context);
//设置对话的标题
builder.setTitle("警告");
//设置对话的图片
builder.setIcon(R.drawable.icon);
//设置对话的主体内容
builder.setMessage("你已经帅破天际了!!");
builder.setPositiveButton("朕知道了", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
System.out.println("确定按钮");
}
});
builder.setNegativeButton("退下吧", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
System.out.println("取消按钮");
}
});
builder.show();

}



}



<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:orientation="vertical"
    tools:context="com.alleged.Alert.MainActivity" >
    <Button 
        android:id="@+id/normalDialog"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="普通对话框"
        android:onClick="normaldialog"
        />
    <Button 
        android:id="@+id/MultipleChoice"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="单项选择对话框"
        android:onClick="Multiplechoice"
        />
    <Button 
        android:id="@+id/multi_select"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="多项选择对话框"
        android:onClick="multiselect"
        />
    <Button 
        android:id="@+id/progressSeclect"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="进度条选择对话框"
        android:onClick="progressSeclect"
        />
</LinearLayout>

0 0
原创粉丝点击