첫날 Dialog 대화 상자

37384 단어

첫 번째 장치 Dialog 대화 상자

  • 일반 대화상자
  • 단일 클릭 대화상자
  • 다중 선택 대화 상자
  • 사용자 지정 대화 상자
  • 수평 진행률 표시줄 대화상자
  • 달력 대화상자
  • 날짜 대화상자
  • 일반 대화상자

      btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // z 
                    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                    builder.setIcon(R.drawable.ic_launcher_background);
                    builder.setTitle(" ");
                    builder.setMessage("w shni ye");
                    // 
                    builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Toast.makeText(MainActivity.this," ok",Toast.LENGTH_SHORT).show();
                        }
                    });
    
                    builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Toast.makeText(MainActivity.this," cancel",Toast.LENGTH_SHORT).show();
                        }
                    });
                    // 
                    AlertDialog alertDialog = builder.create();
                    // 
                    alertDialog.show();
    
                }
            });
    

    단일 클릭 대화상자

     btn_siggle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                    builder.setIcon(R.drawable.ic_launcher_background);// 
                    builder.setTitle(" ");// 
    
    
                    builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Toast.makeText(MainActivity.this," ok",Toast.LENGTH_SHORT).show();
                        }
                    });
                    builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Toast.makeText(MainActivity.this," cancel",Toast.LENGTH_SHORT).show();
                        }
                    });
    
                   final  String[] str={" ", " ", " "};
                    builder.setSingleChoiceItems(str, 2, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Toast.makeText(MainActivity.this, " "+str[which], Toast.LENGTH_SHORT).show();
                        }
                    });
    
    
                    AlertDialog alertDialog = builder.create();
                    alertDialog.show();
    
                }
            });
    

    다중 선택 대화 상자

      // 
            btn_more.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                    // 
                   final String[] str={"ppt","c++","java"};
                    // 
                  final   boolean[] bol={true,false,true};
                    builder.setMultiChoiceItems(str, bol, new DialogInterface.OnMultiChoiceClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                                bol[which]=isChecked;
                        }
                    });
                    AlertDialog alertDialog = builder.create();
                    alertDialog.show();
                }
            });
    

    사용자 지정 대화 상자

      // 
            btn_lay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                    View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.item_1, null);
                    builder.setView(view);
    
                    AlertDialog alertDialog = builder.create();
                    alertDialog.show();
                }
            });
    

    수평 진행률 막대 대화 상자

     // 
            btn_jin.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final ProgressDialog dialog = new ProgressDialog(MainActivity.this);
                    dialog.setMax(100);
                    dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                    dialog.setMessage(" ");
                    // 
                    dialog.show();
                    final Timer timer = new Timer();
                    timer.schedule(new TimerTask() {
                        int progress=0;
                        @Override
                        public void run() {
                            if (progress==100){
                                dialog.dismiss();
                                timer.cancel();
    
                            }
                            dialog.setProgress(progress+=10);
                        }
                    },0,1000);
                }
            });
    

    캘린더 대화 상자

      // 
            btn_date.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                    Calendar calendar = Calendar.getInstance();// 
                    new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
                        @Override
                        public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                            Toast.makeText(MainActivity.this, year+"-"+(month+1)+"-"+dayOfMonth, Toast.LENGTH_SHORT).show();
                        }
                    },calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)).show();
                }
            });
    

    날짜 대화 상자

       // 
            btn_time.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Calendar calendar=Calendar.getInstance();// 
    
                    new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
                        @Override
                        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                            Toast.makeText(MainActivity.this, hourOfDay+":"+minute, Toast.LENGTH_SHORT).show();
                        }
                    },calendar.get(Calendar.HOUR),calendar.get(Calendar.MINUTE),true).show();
    
                }
            });
    

    좋은 웹페이지 즐겨찾기