0

カスタムレイアウトを使用するAlertDialogがあります。OnClickイベントで、ユーザーが入力した内容を読みたいので、次のようにします。

//Login Dialog
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            // Get the layout inflater
            LayoutInflater inflater = this.getLayoutInflater();

            // Inflate and set the layout for the dialog
            // Pass null as the parent view because its going in the dialog layout
            builder.setView(inflater.inflate(R.layout.dialog_signin, null))
            .setPositiveButton(R.string.tvLoginTitle, new DialogInterface.OnClickListener() {
                   @Override
                   public void onClick(DialogInterface dialog, int id) {
                       String username = ((EditText)findViewById(R.id.loginUsername)).getText().toString();
                       String password = ((EditText)findViewById(R.id.loginPassword)).getText().toString();
                       System.out.println(username);
                       System.out.println(password);
                       ConnectionID.loginToServer(cMainActivity, username, password);
                   }
               })
               .setNegativeButton(R.string.btnExit, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       System.exit(0);

                   }
               }).show();  

しかし、内部の内容を読みたい場合は、常にNullPointerExceptionが発生します。dialog_signinからのレイアウトは次のようになります。

<EditText
    android:id="@+id/loginUsername"
    android:inputType="textEmailAddress"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:layout_marginLeft="4dp"
    android:layout_marginRight="4dp"
    android:layout_marginBottom="4dp"
    android:hint="@string/tvUsername" />
<EditText
    android:id="@+id/loginPassword"
    android:inputType="textPassword"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="4dp"
    android:layout_marginLeft="4dp"
    android:layout_marginRight="4dp"
    android:layout_marginBottom="16dp"
    android:fontFamily="sans-serif"
    android:hint="@string/tvPassword"/>
4

3 に答える 3

0

以下を使用します。

View v=inflater.inflate(R.layout.dialog_signin, null);

そして置き換えます:

String username = ((EditText)findViewById(R.id.loginUsername)).getText().toString();
String password = ((EditText)findViewById(R.id.loginPassword)).getText().toString();

String username = ((EditText)v.findViewById(R.id.loginUsername)).getText().toString();
String password = ((EditText)v.findViewById(R.id.loginPassword)).getText().toString();
于 2012-11-06T12:14:25.663 に答える
0

ユーザー名とパスワードがダイアログに属するフィールドである場合、次のコードに示すように実行する必要があります。

編集:

View v = inflater.inflate(R.layout.dialog_signin, null);
builder.setView(v); 
String username = ((EditText)v.findViewById(R.id.loginUsername)).getText().toString();       
String password = ((EditText)v.findViewById(R.id.loginPassword)).getText().toString();
于 2012-11-06T12:11:01.183 に答える
0

次のようにDialogで使用すると便利で、私にとってはうまく機能します。

Dialog builder = new Dialog(this);
builder.setContentView(R.layout.dialog_signin);


String username = ((EditText)builder.findViewById(R.id.loginUsername)).getText().toString();
String password = ((EditText)builder.findViewById(R.id.loginPassword)).getText().toString();

//Your buttons

builder.show();
于 2012-11-06T12:19:14.953 に答える