安卓専高一day 01ダイアログ

41976 ワード

安卓専高一day 01ダイアログ
  • ダイアログ
  • 一般ダイアログ
  • ラジオダイアログ
  • 複数選択ダイアログ
  • カスタムダイアログ
  • 進捗バーダイアログ
  • 日付選択ダイアログ
  • 時間選択ダイアログ
  • ダイアログ
    標準ダイアログ
    //     
        public void a1(View view) {
            //1.      
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
    
            //2.  
            builder.setIcon(R.drawable.ic_launcher_background);//  
            builder.setTitle("    ");//  
            builder.setMessage("      ");//    
            //      
            builder.setPositiveButton("  ", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(MainActivity.this, "     ", Toast.LENGTH_SHORT).show();
                }
            });
            //    
            builder.setNegativeButton("  ", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(MainActivity.this, "     ", Toast.LENGTH_SHORT).show();
                }
            });
    
            //3.          
            AlertDialog dialog = builder.create();
            //4.  
            dialog.show();
        }
    

    ラジオダイアログ
    //     
        public void a2(View view) {
            //1.     
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
    
            //2.  
            builder.setIcon(R.drawable.ic_launcher_background);//  
            builder.setTitle("     ");//  
            //    
            builder.setPositiveButton("  ", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(MainActivity.this, "  ", Toast.LENGTH_SHORT).show();
                }
            });
            //    
            builder.setNegativeButton("  ", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(MainActivity.this, "  ", Toast.LENGTH_SHORT).show();
                }
            });
            //    
            final String[] items = {"  ","  ","  "};
            //                            
           builder.setSingleChoiceItems(items, 2, new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int which) {
                   Toast.makeText(MainActivity.this, "    "+items[which], Toast.LENGTH_SHORT).show();
               }
           });
           //3.          
            AlertDialog dialog = builder.create();
            //4.  
            dialog.show();
        }
    

    複数選択ダイアログ
    //     
        public void a3(View view) {
            //1.     
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            final String[] strings = {"  ","  ","  "};//    
            final boolean[] booleans = {true,true,false};//      
            //  
            builder.setIcon(R.drawable.ic_launcher_background);//  
            builder.setTitle("     ");//  
            //    
            builder.setPositiveButton("  ", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    for (int i = 0; i< booleans.length; i++){
                        if (booleans[i]){
                            Toast.makeText(MainActivity.this, "    "+strings[i], Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            });
            //    
            builder.setNegativeButton("  ", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(MainActivity.this, "   ", Toast.LENGTH_SHORT).show();
                }
            });
            //      
            builder.setMultiChoiceItems(strings, booleans, new DialogInterface.OnMultiChoiceClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                    booleans[which] = isChecked;//    
                }
            });
            //3.          
            AlertDialog dialog = builder.create();
            //4.  
            dialog.show();
        }
    

    ダイアログのカスタマイズ
    まずレイアウトをカスタマイズ(zdy.xml)
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >
    
        <TextView
            android:textSize="30sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="   "
            android:layout_gravity="center"
            />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher_foreground"
            android:layout_gravity="center"
            />
    </LinearLayout>
    

    カスタムレイアウトを参照するjavaコード
    //      
        public void a4(View view) {
            //1.    
            View view1 = LayoutInflater.from(this).inflate(R.layout.zdy,null);
            //2.     
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            //3.    
            builder.setView(view1);
            //4.          
            AlertDialog dialog = builder.create();
            //5.  
            dialog.show();
    

    進捗バーダイアログ
    //      
        public void a5(View view) {
            //1.     
            final ProgressDialog dialog = new ProgressDialog(this);
            //    
            dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//     
           // dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);//      
            dialog.setMax(100);//   
            dialog.setProgress(0);//   
            dialog.setMessage("    ");
            dialog.show();
            //     
            final Timer timer = new Timer();
            timer.schedule(new TimerTask() {
                int n = 0;
                @Override
                public void run() {
                    if (n == 100){
                        dialog.dismiss();//  
                        timer.cancel();
                    }
                    dialog.setProgress(n+=10);
                }
            },0,1000);
        }
    

    日付選択ダイアログ
     //       
        public void a6(View view) {
            Calendar calendar = Calendar.getInstance();//    
            new DatePickerDialog(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.MONDAY),calendar.get(Calendar.DAY_OF_MONTH)).show();
    

    時間選択ダイアログ
    //       
        public void a7(View view) {
            Calendar calendar = Calendar.getInstance();//    
            new TimePickerDialog(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();//ture  24   
        }