Android中的常用Dialog

//  设置基本对话框  
public void btn_dialog1(View v) {
// 用内部类实例化dialog的对象
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
// 设置参数信息
dialog.setTitle("很简便的dialog");
dialog.setMessage("好东西");
dialog.setPositiveButton("确定", null);
dialog.setNegativeButton("取消", null);
dialog.create();
dialog.show();
}
// 设置成为输入文本对话框
public void btn_dialog2(View v){
//          用内部类实例化dialog的对象
   AlertDialog.Builder dialog = new AlertDialog.Builder(this);
   dialog.setTitle("请输入");
//          设置一个小图标
   dialog.setIcon(R.drawable.ic_launcher);
//          设置输入框
   dialog.setView(new EditText(this));
   dialog.setPositiveButton("确定", null);
   dialog.setNeutralButton("取消", null);
   dialog.create();
   dialog.show();


}

//设置输入框的dialog
public void btn_onclock(View v) {
/**
* 在一个xml的布局文件叫dialoglayout
* dialoglayout有两个EditText
*/
LayoutInflater inflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View view = inflate.inflate(R.layout.dialoglayout, null);
// 用内部类实例化dialog的对象
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("最终确认信息");
// 设置输入框
dialog.setView(view);
dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

EditText e1 = (EditText) view.findViewById(R.id.editText1);
EditText e2 = (EditText) view.findViewById(R.id.editText2);
Toast.makeText(MainActivity.this, e1.getText().toString().trim(), 10).show();
Toast.makeText(MainActivity.this, e2.getText().toString().trim(), 10).show();
}
});
dialog.setNeutralButton("取消", null);

dialog.create();
dialog.show();


}

//设置成为单选框
public void btn_dialog3(View v){
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("请选择");
String[]items=new String[]{"北京","上海","广州","深圳","西安"};
dialog.setSingleChoiceItems(items, 0, null);
dialog.create();
dialog.show();
}
// 设置成为多选框
public void btn_dialog4(View v){
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("请选择");
String[]items=new String[]{"北京","上海","广州","深圳","西安"};
dialog.setMultiChoiceItems(items,new boolean[]{true,false,false,false,false}, null);
dialog.create();
dialog.show();
}
// 设置成为列表对话框
public void btn_dialog5(View v){
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("请选择");
String[]items=new String[]{"北京","上海","广州","深圳","西安"};
dialog.setItems(items, new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
//which参数表示的是被选项在数组中的下标位置。
System.out.println(which+">>>>>>>");

}
});



dialog.create();
dialog.show();

}

AlertDialog对象的使用,引入布局,自定义确定,取消按钮

void myDialog(){
final AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("警告:");
alertDialog.setCancelable(false);
//放入布局文件
LayoutInflater inflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflate.inflate(R.layout.fragment_quchec_button, null);
//确定按钮
    final Button quedingBtn=(Button) view.findViewById(R.id.f_quchecbutton_btn_queding);
quedingBtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "确定按钮被点击了", 10).show();
quedingBtn.setEnabled(false);
alertDialog.dismiss();
}
});
//取消按钮
Button quxiaoBtn=(Button) view.findViewById(R.id.f_quchecbutton_btn_quxiao);
quxiaoBtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "取消按钮被点击了", 10).show();
alertDialog.dismiss();
}
});
alertDialog.setView(view);
alertDialog.show();

 
}//myDialog

布局:fragment_quchec_button.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
     
      <TextView      
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请确认,客户微信支付成功或者收到现金了吗?"
        android:textSize="20sp"
        />
  
<LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"    
    >
    
     <Button
        android:id="@+id/f_quchecbutton_btn_quxiao"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="取消" 
        android:layout_weight="1"
        />
      <Button
        android:id="@+id/f_quchecbutton_btn_queding"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="确定"
        android:layout_weight="1"
         />
    
    
    
</LinearLayout>
    
</LinearLayout>








//ProgressDialog进度条对话框

ProgressDialog progressDialog2 =new ProgressDialog(this);

progressDialog2.setTitle("结算");
progressDialog2.setMessage("拼命加载中。。。");
progressDialog2.setCancelable(true);
progressDialog2.show();//对话框显示

progressDialog2.dismiss();//对话框消失



//吐丝也是对话框,自定义吐丝
public void btn_dialog6(View v) {
LayoutInflater inflater = getLayoutInflater();

View layout = inflater.inflate(R.layout.custom_toast,
                              (ViewGroup) findViewById(R.id.toast_layout_root));


TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("我写的自定义吐丝,是从文档中复制过来的");


Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
// 设置时间
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();

}

如果还不清楚的话免积分下载我的例子

http://download.csdn.net/detail/zhaihaohao1/8253149

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值