Androidの簡単なカスタムダイアログボックスの生産

レイアウトファイル

<?xml version = "1.0" コード= "UTF-8"?>
<TableLayoutのxmlns:アンドロイド= "http://schemas.android.com/apk/res/android"
    xmlns:ツール= "http://schemas.android.com/tools"
    アンドロイド:layout_width = "match_parent" のandroid:layout_height = "match_parent">

    <のTableRow
        アンドロイド:layout_width = "397dp"
        アンドロイド:layout_height = "131dp">

        <TextViewの
            アンドロイド:ID = "@ + ID / namet"
            ツール:テキスト= "姓名">

        </ TextViewの>

        <のEditTextアンドロイド:ID = "@ + ID / edname"> </のEditText>
    </のTableRow>
</ TableLayout>

 Mainactivityバインドボタンイベント(メインレイアウトが与えられていません)

最終ボタンDIY =(ボタン)findViewById(R.id.diy)。
        diy.setOnClickListener(新しいView.OnClickListener(){
            @オーバーライド
            公共のボイドのonClick(ビューV){
                LayoutInflater flater = LayoutInflater.from(MainActivity.this)。
                最終ビューdialogview = flater.inflate(R.layout.login、NULL); //ビューにレイアウトファイル
                ダイアログ]ダイアログ=新AlertDialog.Builder(MainActivity.this).setTitle( "ログイン")//セットタイトル
                        .setView(dialogview)
                        .setPositiveButton( "OK"
                                新しいDialogInterface.OnClickListener(){
                                    @オーバーライド
                                    公共のボイドのonClick(DialogInterfaceダイアログ、INT){
                           EditTextは(のEditText)dialogview.findViewById(R.id.edname)=編; //ポップのアセンブリを取得します
                                        ストリングMSG = ed.getText()のtoString()。
                                                                   Toast.makeText(getApplicationContext()、MSG、Toast.LENGTH_SHORT).SHOW();
                                    }
                                })。setNegativeButton( "取消"、新DialogInterface.OnClickListener(){
                            @オーバーライド
                            公共のボイドのonClick(DialogInterfaceダイアログ、INT){

                            }
                        。}))(作ります。
                dialog.show();
            }
        });

  レンダリング:

 

おすすめ

転載: www.cnblogs.com/liuleliu/p/12288390.html