Android中Dialog(对话框)的用法

来源:互联网 发布:数据展现工具 编辑:程序博客网 时间:2024/05/19 17:48
一、使用系统自带的Dialog布局如果要想实例化AlertDialog类往往都依靠其内部类:AlertDialog.Builder完成。
.xml <Button       android:id="@+id/btn_sysdialog"       android:layout_width="wrap_content"       android:layout_height="wrap_content"       android:text="系统dialog测试"/>.javapublic 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();    }
0 0
原创粉丝点击