6

デフォルトのAlertDialogに2つの要素を追加しようとしていましたが、動作させることができません。ここに私のコードだ:今のところデフォルトのAlertDialogをプログラムで使用してTextViewとEditTextを追加する方法

// START Dialog 
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this); 

    TextView tv = new TextView(this); 
    tv.setText(title); 
    tv.setPadding(40, 40, 40, 40); 
    tv.setGravity(Gravity.CENTER); 
    tv.setTextSize(20); 

    EditText et = new EditText(this); 
    etStr = et.getText().toString(); 

    alertDialogBuilder.setView(et); 
    alertDialogBuilder.setTitle(title); 
    alertDialogBuilder.setMessage("Input Student ID"); 
    alertDialogBuilder.setCustomTitle(tv); 

    if (isError) 
     alertDialogBuilder.setIcon(R.drawable.icon_warning); 
    // alertDialogBuilder.setMessage(message); 
    alertDialogBuilder.setCancelable(false); 

    // Setting Negative "Cancel" Button 
    alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int whichButton) { 
      dialog.cancel(); 
     } 
    }); 

    // Setting Positive "Yes" Button 
    alertDialogBuilder.setPositiveButton("OK", 
      new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        if (isError) 
         finish(); 
        else { 
         Intent intent = new Intent(
           ChangeDeviceActivity.this, 
           MyPageActivity.class); 
         startActivity(intent); 
        } 
       } 
      }); 

    AlertDialog alertDialog = alertDialogBuilder.create(); 

    try { 
     alertDialog.show(); 
    } catch (Exception e) { 
     // WindowManager$BadTokenException will be caught and the app would 
     // not display the 'Force Close' message 
     e.printStackTrace(); 
    } 

、これが唯一のEditTextalertDialogBuilder.setMessage("Input Student ID");によって設定されたメッセージであるが、私は、これはTextViewので、私はセンター正当化することができ、それを作りたいです。これはどうすればいいですか?

答えて

23
 AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this); 

     LinearLayout layout = new LinearLayout(this); 
     LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
     layout.setOrientation(LinearLayout.VERTICAL); 
     layout.setLayoutParams(parms); 

     layout.setGravity(Gravity.CLIP_VERTICAL); 
     layout.setPadding(2, 2, 2, 2); 

     TextView tv = new TextView(this); 
     tv.setText("Text View title"); 
     tv.setPadding(40, 40, 40, 40); 
     tv.setGravity(Gravity.CENTER); 
     tv.setTextSize(20); 

     EditText et = new EditText(this); 
     etStr = et.getText().toString(); 
     TextView tv1 = new TextView(this); 
     tv1.setText("Input Student ID"); 

     LinearLayout.LayoutParams tv1Params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
     tv1Params.bottomMargin = 5; 
     layout.addView(tv1,tv1Params); 
     layout.addView(et, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 

     alertDialogBuilder.setView(layout); 
     alertDialogBuilder.setTitle(title); 
     // alertDialogBuilder.setMessage("Input Student ID"); 
     alertDialogBuilder.setCustomTitle(tv); 

     if (isError) 
      alertDialogBuilder.setIcon(R.drawable.icon_warning); 
     // alertDialogBuilder.setMessage(message); 
     alertDialogBuilder.setCancelable(false); 

     // Setting Negative "Cancel" Button 
     alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int whichButton) { 
       dialog.cancel(); 
      } 
     }); 

     // Setting Positive "OK" Button 
     alertDialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
       if (isError) 
        finish(); 
       else { 
         Intent intent = new Intent(ChangeDeviceActivity.this, 
         MyPageActivity.class); startActivity(intent); 
       } 
      } 
     }); 

     AlertDialog alertDialog = alertDialogBuilder.create(); 

     try { 
      alertDialog.show(); 
     } catch (Exception e) { 
      // WindowManager$BadTokenException will be caught and the app would 
      // not display the 'Force Close' message 
      e.printStackTrace(); 
     } 
+0

しかし、私は 'layout.setOrientation(LinearLayout.HORIZONTAL);を' layout.setOrientation(LinearLayout.VERTICAL); 'に変更したので、' tv1'をEditTextの上に置いて 'tv1'と' et 'は表示されませんか? –

+1

は、垂直方向の回答を編集します。 –

+0

ありがとうございます!本当にありがとう。編集した部分の+1。 –

1

setView()を呼び出すと、メッセージを保持している元のTextViewが非表示になります。 あなたは

  • を眺めながら何かを膨らま
  • findViewById()
  • とビューの参照を取得レイアウトを小さなレイアウトXMLを作成し、それ
  • へのEditTextとのTextViewを入れ

    に必要
+0

しかし、私はアンドロイドのデフォルトアラートダイアログレイアウトやホロライトテーマを取得できませんでした。 –

+0

なぜですか?デフォルトのAlertDialogテーマは外側に設定されています。プレーンなEditTextではなく、子を持つビューを渡すだけです。 – flx

2

以下のようなビューを含む1つの.xmlファイルを作成します。コードは以下のあなたの.javaファイルの実装でその後

<?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:id="@+id/textView1" 
     android:layout_width="312dp" 
     android:layout_height="wrap_content" 
     android:text="Enter your email address :" 
     android:layout_marginLeft="5dp" 
     android:textAppearance="?android:attr/textAppearanceSmall"/> 

<EditText 
     android:id="@+id/dialog1Edittext" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:layout_marginTop="10dp" 
     android:inputType="textEmailAddress"> 

    <requestFocus/> 
</EditText> 

View view = View.inflate(this, R.layout.yourxmlname, null); 
AlertDialog.Builder alert = new AlertDialog.Builder(this); 
// Now set the dialog's content 
alert.setContentView(view); 

希望します。

+0

ありがとうございますが、デフォルトの警告ダイアログを使用してプログラムで追加する必要がありました。 –

関連する問題