Android中Dialog(对话框)的用法

一、使用系统自带的Dialog布局

如果要想实例化AlertDialog类往往都依靠其内部类:AlertDialog.Builder完成。
.xml

 <Button
       android:id="@+id/btn_sysdialog"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="系统dialog测试"/>
.java

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private Button btn_sysdialog,btn_customdialog;
    private EditText name,pwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_sysdialog= (Button) findViewById(R.id.btn_sysdialog);
        btn_sysdialog.setOnClickListener(this);
        btn_customdialog= (Button) findViewById(R.id.btn_customdialog);
        btn_customdialog.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_sysdialog:
                    showsysdialog();
                break;
            case R.id.btn_customdialog:
                showCustomDialog();
                break;
        }

    }
    public void showsysdialog(){
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setIcon(android.R.drawable.ic_dialog_alert).setTitle("标题").setMessage("是否删除?")
        .setPositiveButton("删除", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this,"确认删除",Toast.LENGTH_SHORT).show();
            }
        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this,"取消",Toast.LENGTH_SHORT).show();
            }
        }).setNeutralButton("查看详情", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this,"查看详情",Toast.LENGTH_SHORT).show();
            }
        });
        builder.create().show();
    }
二、使用自定义布局
custum.xml

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="用户名:"
            android:textSize="25sp"/>
        <EditText
            android:id="@+id/et_username"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="请输入用户名"
            android:textSize="25sp"/>
    </LinearLayout>。。。。。。

.java

 public void showCustomDialog(){
        LayoutInflater inflater=LayoutInflater.from(this);//加载布局
        View view=inflater.inflate(R.layout.custom_dialog,null);
//        在子布局中通过ID实例化控件,必须加上加载过的view视图来调用,不然会报空指针异常
        name= (EditText)view.findViewById(R.id.et_username);
        pwd= (EditText) view.findViewById(R.id.et_pwd);
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setView(view);
        builder.setTitle("登录");
        builder.setPositiveButton("确认登录", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //在此处拿到用户输入的账号密码
                String username=name.getText().toString();
                String password=pwd.getText().toString();
                Toast.makeText(MainActivity.this,"username:"+username+",pwd:"+password,Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("取消",null);
        builder.create().show();
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值