安卓简易对话框

by admin on 2020年1月5日
package com.example.dialog;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
    protected static final int DIALOG11 = 1;
    protected static final int DIALOG111 = 2;
    protected static final int DIALOG1 = 3;
    private Button button1;
    private Button button2;
    private Button button3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findview();
        listener();
        button1 = (Button) findViewById(R.id.button1);
        button2 = (Button) findViewById(R.id.button2);
        button3 = (Button) findViewById(R.id.button3);

    }

    // 绑定按钮
    private void findview() {
        button1 = (Button) findViewById(R.id.button1);
        button2 = (Button) findViewById(R.id.button2);
        button3 = (Button) findViewById(R.id.button3);
    }

    // 监听集
    private void listener() {
        button2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // buildDialog2(MainActivity.this);
                buildDialog2(MainActivity.this);
            }
        });

        button1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                buildDialog1(MainActivity.this);
            }
        });

        button3.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                buildDialog3(MainActivity.this);

            }

        });

    }

    private void buildDialog2(Context context) {

        AlertDialog.Builder builder = new Builder(MainActivity.this);

        builder.setTitle("二个");
        builder.setMessage("ok");
        builder.setPositiveButton("确定", null);
        builder.setNegativeButton("取消", null);

        Dialog DIALOG2 = builder.create();
        DIALOG2.show();

        return;

    }

    private void buildDialog1(Context context) {

        AlertDialog.Builder builder = new Builder(MainActivity.this);

        builder.setTitle("how are you");
        builder.setMessage(getLocalClassName());

        Dialog DIALOG1 = builder.create();
        DIALOG1.show();

    }

    private void buildDialog3(Context context) {
        AlertDialog.Builder builder = new Builder(MainActivity.this);
        builder.setTitle("列表对话框");
        builder.setMultiChoiceItems(new String[] { "选项1", "选项2", "选项3" }, null,
                null);
        builder.setPositiveButton(R.string.app_name,
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface arg0, int whichButton) {
                        setTitle("点击了对话框上的确定按钮");
                    }
                });

        Dialog DIALOG3 = builder.create();
        DIALOG3.show();

    }

    @Override
    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;
    }

}

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图