ほとんどの科学的なライティングの使用アンドリュースAlertDialogダイアログの4種類

まず、マップ上:

 

 XMLコードは、ボタンの製造のために、以下の通りです。

<?XMLバージョン= "1.0"エンコード= "UTF-8" ?> 
< のLinearLayout のxmlns:アンドロイド= "http://schemas.android.com/apk/res/android" 
のandroid:layout_width = "match_parent" 
のandroid:layout_height =」 match_parent " 
アンドロイド:背景= "@描画可能/背景" 
のxmlns:ウィジェット= "http://schemas.android.com/apk/res-auto" 
アンドロイド:オリエンテーション= "縦" > 
    < ボタン
         アンドロイド:ID =" @ + ID / button_1" 
        アンドロイド:layout_width = "match_parent" 
        のandroid:layout_height =」
        
    < ボタン
         アンドロイド:ID = "@ + ID / button_2" 
        アンドロイド:layout_width = "match_parent" 
        のandroid:layout_height = "wrap_content" 
        アンドロイド:テキスト= "列表的ダイアログ" 
        /> 
    < ボタン
         アンドロイド:ID = "@ + ID / button_3を" 
        アンドロイド:layout_width =" match_parent」
        のandroid:layout_height = "wrap_content" 
        アンドロイド:テキスト= "单选的ダイアログ" 
        /> 
    < ボタン
         アンドロイド:ID = "@ + ID / button_4" 
        アンドロイド:layout_width = "match_parent"
        アンドロイド:layout_height = "wrap_content"
        アンドロイド:テキスト= "複数選択ダイアログ" 
        /> 

</ のLinearLayout >

ロジックを実装するために、次のようにJavaコードは次のとおりです。

輸入androidx.appcompat.app.ActionBar;
輸入androidx.appcompat.app.AlertDialog;
輸入androidx.appcompat.app.AppCompatActivity;
輸入android.content.DialogInterface;
輸入android.os.Bundle;
輸入android.view.View;
輸入android.widget.Button;
輸入android.widget.Toast; 



パブリック クラス MainActivityは延びAppCompatActivity {
     int型のインデックスを、
    文字列[]アイテム = { "アンドロイド"、 "IOS"、 "スパーク"、 "Hadoopの"、 "ウェブ" }。
    ブール [] bools = { }。
    // 设置ブール数组所有的选项设置默认没选
    @Override
     保護 ボイドのonCreate(バンドルsavedInstanceState){
         スーパー.onCreate(savedInstanceState)。
        setContentView(R.layout.activity_main)。


        アクションバーアクションバー = getSupportActionBar()。
        もし(!アクションバー= ヌル){ 
            actionBar.hide(); 
        } 
        ボタンのボタン = (ボタン)findViewById(R.id.button_1)。
        button.setOnClickListener(View.OnClickListener(){ 
            @Override 
            公共 のボイドのonClick(ビュービュー){ 
                AlertDialog.Builderビルダー = 新しい AlertDialog.Builder(MainActivity。この); 
                builder.setIcon(R.drawable.girl)。

                builder.setTitle( "标题栏" ); 
                builder.setMessage( "对话框内容、可自行设置" ); 
                builder.setPositiveButton( "确定"、DialogInterface.OnClickListener(){ 
                    @Override 
                    公共 のボイドのonClick(DialogInterfaceダイアログ、int型){
                        Toast.makeText(MainActivity。この、 "OKをクリック" .SHOW()、Toast.LENGTH_SHORT); 
                    } 
                }); 
                builder.setNegativeButtonは( "キャンセル"、新新DialogInterface.OnClickListenerを(){ 
                    @Override 
                    公共 のボイドのonClick(DialogInterface dialogInterface 、int型I){ 
                        Toast.makeText(MainActivity。この、 "キャンセルをクリック" 、Toast.LENGTH_SHORT).SHOW(); 
                    } 
                }); 
                builder.setNeutralButton( "良い"、新新DialogInterface.OnClickListener(){ 
                    @Override 
                    公共 ボイドのonClick(DialogInterface dialogInterfaceは、int型のI){ 
                        Toast.makeText(MainActivity。この、 "点击了『好的』" 、Toast.LENGTH_SHORT).SHOW(); 
                    } 
                })。
                AlertDialog alertDialog = builder.create()。
                alertDialog.show(); 
            } 
        })。

        ボタンボタン2 = (ボタン)findViewById(R.id.button_2)。
        button2.setOnClickListener(新しい View.OnClickListener(){
            @Override 
            公衆 のボイドのonClick(ビュービュー){ 

                AlertDialog.Builderビルダー = 新しい AlertDialog.Builder(MainActivity。この); 
                builder.setTitle( "请选择一个技术分支" ); 
                builder.setItems(項目、新規DialogInterface.OnClickListener(){ 
                    @Override 
                    公共 ボイドのonClick(DialogInterfaceダイアログ、INT ){ 
                        Toast.makeText(MainActivity。この、 "选择了" + アイテム[]、Toast.LENGTH_SHORT).SHOW (); 
                    }
                }); 
                // 取消可以不添加
                 // builder.setNegativeButton( "取消"は、null); 
                AlertDialog alertDialog = builder.create()。
                alertDialog.show(); 
            } 
        })。
        ボタンボタン3 = (ボタン)findViewById(R.id.button_3)。

        button3.setOnClickListener(新しいView.OnClickListener(){ 
            @Override 
            公共 のボイドのonClick(ビュービュー){ 

                AlertDialog.Builderビルダー = 新しい AlertDialog.Builder(MainActivity。この); 
                builder.setTitle( "请选择技术分支:" );
                builder.setSingleChoiceItems(項目、インデックス、新しいDialogInterface.OnClickListener(){ 
                    @Override 
                    公共 ボイドのonClick(DialogInterfaceダイアログ、INT ){ 
                        インデックス = ; 
                    } 
                })。
                builder.setPositiveButton( "确定"、DialogInterface.OnClickListener(){ 
                    @Override 
                    公共 のボイドのonClick(DialogInterfaceダイアログ、INT ){ 
                        Toast.makeText(MainActivity。この、 "选择了" +項目[インデックス]、Toast.LENGTH_SHORT)(.SHOW)。
                    } 
                })。
                builder.setNegativeButton( "取消"、ヌル); 
                AlertDialog alertDialog = builder.create()。
                alertDialog.show(); 
            } 
        })。
        ボタンボタン4 = (ボタン)findViewById(R.id.button_4)。
        button4.setOnClickListener(新しいView.OnClickListener(){ 
            @Override 
            公共 のボイドのonClick(ビュービュー){ 
                AlertDialog.Builderビルダー = 新しいですAlertDialog.Builder(MainActivity。この); 
                builder.setTitle( "请选择技术分支:" ); 
                builder.setMultiChoiceItems(項目、bools、新しいDialogInterface.OnMultiChoiceClickListener(){ 
                    @Override 
                    公共 ボイドのonClick(DialogInterfaceダイアログ、整数ブールにisChecked){ 
                        bools [] = にisChecked; 
                    } 
                })。
                builder.setPositiveButton( "确定"、DialogInterface.OnClickListener(){ 
                    @Override
                    公共 のボイドのonClick(DialogInterfaceダイアログ、int型){ 
                        StringBufferのSB = 新しいStringBufferを();
                        以下のためにINT ; I <item.length; iが0 = I ++ ){
                             場合(bools [I]){ 
                                sb.append(項目[I] + "" )。
                            } 
                        } 
                        Toast.makeText(MainActivity。この、 "选择了" + sb.toString()、Toast.LENGTH_SHORT).SHOW(); 
                    } 
                })。
                builder.setNegativeButton( "取消"、ヌル); 
                AlertDialog alertDialog = builder.create()。
                alertDialog.show(); 
            } 
        })。
    } 







}

また、解決策を持っています

 

おすすめ

転載: www.cnblogs.com/geeksongs/p/11922012.html