天天看點

Dialog 各種風格使用

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;

import java.util.Calendar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.button)
    Button button;
    @BindView(R.id.button2)
    Button button2;
    @BindView(R.id.button3)
    Button button3;
    @BindView(R.id.button4)
    Button button4;
    @BindView(R.id.button5)
    Button button5;
    @BindView(R.id.button6)
    Button button6;
    @BindView(R.id.button7)
    Button button7;
    @BindView(R.id.button8)
    Button button8;
    @BindView(R.id.activity_main)
    RelativeLayout activityMain;

    Context context;
    private int year;
    private int month;
    private int day;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        context = this;
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @OnClick({R.id.button, R.id.button2, R.id.button3, R.id.button4, R.id.button5, R.id.button6, R.id.button7, R.id.button8})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.button:
                Dialog dialog = new Dialog(this);
                dialog.setTitle("Dialog");
                dialog.setCanceledOnTouchOutside(true);
                dialog.show();

                break;
            case R.id.button2:
                Dialog dialog1 = new AlertDialog.Builder(context).setTitle("AlertBuilder").
                        setMessage("測試測試測試車市速度發順豐").setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(context, "取消了", Toast.LENGTH_SHORT).show();
                    }
                }).setNeutralButton("中間", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(context, "中間取消了", Toast.LENGTH_SHORT).show();
                    }
                }).setPositiveButton("确認", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(context, "确定", Toast.LENGTH_SHORT).show();
                    }
                }).create();
                dialog1.show();
                break;
            case R.id.button3:
                Dialog dialog2 = new android.support.v7.app.AlertDialog.Builder(context).setTitle("AlertBuilder").
                        setMessage("測試測試測試車市速度發車市速度發順豐").setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(context, "取消了", Toast.LENGTH_SHORT).show();
                    }
                }).setNeutralButton("中間", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(context, "中間取消了", Toast.LENGTH_SHORT).show();
                    }
                }).setPositiveButton("确認", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(context, "确定", Toast.LENGTH_SHORT).show();
                    }
                }).setIcon(R.mipmap.ic_launcher).create();
                dialog2.show();
                break;
            case R.id.button4:
                Dialog dialog3 = new android.support.v7.app.AlertDialog.Builder(context).setTitle("輸入框").
                        setView(new EditText(context)).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(context, "取消了", Toast.LENGTH_SHORT).show();
                    }
                }).setPositiveButton("确認", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(context, "确定", Toast.LENGTH_SHORT).show();
                    }
                }).setIcon(R.mipmap.ic_launcher).create();
                dialog3.show();
                break;
            case R.id.button5:
                final String[] str = new String[]{"item1", "item2"};
                Dialog dialog4 = new android.support.v7.app.AlertDialog.Builder(context).setTitle("單選框").
                        setMultiChoiceItems(str, null, new DialogInterface.OnMultiChoiceClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                                StringBuffer sb = new StringBuffer();
                                if (isChecked) {
                                    sb.append(str[which].toString() + ",");
                                }
                                Toast.makeText(context, sb.toString(), Toast.LENGTH_SHORT).show();
                            }
                        }).
                        setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(context, "取消了", Toast.LENGTH_SHORT).show();
                            }
                        }).setPositiveButton("确認", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(context, "确定", Toast.LENGTH_SHORT).show();
                    }
                }).setIcon(R.mipmap.ic_launcher).create();
                dialog4.show();
                break;
            case R.id.button6:
                final ProgressDialog dialog5 = new ProgressDialog(context);
                dialog5.setMessage("下載下傳最新apk");
                dialog5.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                dialog5.setMax(100);
                dialog5.show();
                new Thread(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        int i = 0;
                        while (i < 100) {
                            try {
                                Thread.sleep(200);
                                // 更新進度條的進度,可以在子線程中更新進度條進度
                                dialog5.incrementProgressBy(1);
                                // dialog.incrementSecondaryProgressBy(10)//二級進度條更新方式
                                i++;

                            } catch (Exception e) {
                                // TODO: handle exception
                            }
                        }
                        // 在進度條走完時删除Dialog
                        dialog5.dismiss();

                    }
                }).start();
                break;
            case R.id.button7:
                //初始化Calendar月曆對象
                Calendar mycalendar = Calendar.getInstance();

                //擷取Calendar對象中的年
                year = mycalendar.get(Calendar.YEAR);
                //擷取Calendar對象中的月
                month = mycalendar.get(Calendar.MONTH);
                //擷取這個月的第幾天
                day = mycalendar.get(Calendar.DAY_OF_MONTH);
                Toast.makeText(context, "目前日期:" + year + "-" + (month + 1) + "-" + day, Toast.LENGTH_SHORT).show();
                DatePickerDialog dialog6 = new DatePickerDialog(context, Datelistener, year, month, day);
                dialog6.show();//顯示DatePickerDialog元件
                break;
            case R.id.button8:
                break;
        }
    }

    private DatePickerDialog.OnDateSetListener Datelistener = new DatePickerDialog.OnDateSetListener() {
        /**params:view:該事件關聯的元件
         * params:myyear:目前選擇的年
         * params:monthOfYear:目前選擇的月
         * params:dayOfMonth:目前選擇的日
         */
        @Override
        public void onDateSet(DatePicker view, int myyear, int monthOfYear, int dayOfMonth) {


            //修改year、month、day的變量值,以便以後單擊按鈕時,DatePickerDialog上顯示上一次修改後的值
            year = myyear;
            month = monthOfYear;
            day = dayOfMonth;
            //更新日期
            updateDate();

        }

        //當DatePickerDialog關閉時,更新日期顯示
        private void updateDate() {
            //在TextView上顯示日期
            Toast.makeText(context, "目前日期:" + year + "-" + (month + 1) + "-" + day, Toast.LENGTH_SHORT).show();
        }
    };

}