10

私は独学の初心者であり、忍耐に感謝します! ありがとう!

Eclipse で、独自の xml ファイル ("custom_dialog") を使用してカスタム アラート ダイアログを作成しました。これは "usernamealert" と呼ばれます。

ユーザーがまだユーザー名を入力していない場合 (つまり、username.length == 0) にアラートがポップアップするようにします。

このレイアウト内には、textView (「あなたの名前は何ですか?」)、editText、およびボタン (「usernameButton」) があります。

ボタンの onclicklistener を配置する前は、すべてが機能していました。これは私の(関連する)Javaでした:

LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.custom_dialog, (ViewGroup)     getCurrentFocus());
AlertDialog.Builder usernamebuilder = new AlertDialog.Builder(this);
usernamebuilder.setView(dialoglayout);

AlertDialog usernamealert = usernamebuilder.create();

onclicklistenerを入れたら壊れた!どこに置くべきだったの?

(以下は私が試したものです...すべて私のOnCreateで)

LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.custom_dialog, (ViewGroup)getCurrentFocus());
AlertDialog.Builder usernamebuilder = new AlertDialog.Builder(this);
usernamebuilder.setView(dialoglayout);


Button usernameButton = (Button) usernamealert.findViewById(R.id.usernameButton);
usernameButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {  
//store username in sharedprefs
usernamealert.dismiss();



}
});

コードの後に​​私は言った:

if (username.length() == 0) {
            usernamealert.show();
        }

繰り返しますが、ボタンをいじり始める前に機能しました!!

4

2 に答える 2

11

コードがボタンを検索する場所を指定する必要があります。「findViewById」のみがホストの xml で検索される場合は、次のようにする必要があります。

LayoutInflater inflater =getLayoutInflater();
                View myview = inflater.inflate(R.layout.dialoghireteachernegotiate, null);
                // Inflate and set the layout for the dialog
                // Pass null as the parent view because its going in the dialog layout
                builder.setView(myview);
                 Button addS = (Button) myview.findViewById (R.id.bAddS);

これは、hireteachernegotiate.xml のレイアウトを持つ、私のクラス Hireteachernegotiate.class の一部です。

 AlertDialog.Builder builder = new AlertDialog.Builder(this);
                // Get the layout inflater
                LayoutInflater inflater =getLayoutInflater();
                View myview = inflater.inflate(R.layout.dialoghireteachernegotiate, null);
                // Inflate and set the layout for the dialog
                // Pass null as the parent view because its going in the dialog layout
                builder.setView(myview);

                Button addS = (Button) myview.findViewById (R.id.bAddS);
                addS.setOnClickListener(new View.OnClickListener() {

                    public void onClick(View v) {
                        //do some stuff
                    }
                });

                Button minusS = (Button) myview.findViewById (R.id.bMinusS);
                addS.setOnClickListener(new View.OnClickListener() {

                    public void onClick(View v) {
                       //do other stuff
                    }
                });

                // Add action buttons
                       builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog, int id) {


                               dialog.cancel();
                           }
                       });
                   builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                           dialog.cancel();
                       }
                   });   



            builder.show();

これはダイアログのレイアウトです

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <Button
        android:id="@+id/bAddS"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />


    <Button
        android:id="@+id/bMinusS"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>
于 2013-01-09T08:36:40.600 に答える
2

これを試して。

usernamebuilder.setCancelable(false)
usernamebuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
            //do what you want.
       }
   });

それが機能するかどうか、または何らかの形で役立つかどうかを確認してください。

于 2012-08-28T22:36:44.697 に答える