2014-10-20 34 views
0

我試圖創建一個警告對話框,頂部有一個圖像標題,底部有2個按鈕,我想從代碼中設置文本。但是我不能得到橫幅下的設置文本。我想創建類似於這個鏈接上的東西,但是文本總是在圖片上方結束,因爲我不知道如何引用對話框構建器中的textview。由於Android在警報對話框中設置文本

http://developer.android.com/guide/topics/ui/dialogs.html#CustomLayout

AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    LayoutInflater inflater = getLayoutInflater(); 

    builder.setView(inflater.inflate(R.layout.custom_dialog, null)) 


      .setMessage("Message" + message) 

      .setPositiveButton("Share", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 

        if (isNetworkAvailable()){ 

         if (......; 

         } else { 
          ...... 
         } 

        } else { 
         .......; 
        }  
       } 
      }) 
      .setNegativeButton("Shake again!", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        // User cancelled the dialog 

       } 

     }).setOnCancelListener(new OnCancelListener() { 

      @Override 
      public void onCancel(DialogInterface dialog) { 

      } 
     }); 



    AlertDialog alert = builder.create(); 

    alert.setOnDismissListener(new OnDismissListener() { 

     @Override 
     public void onDismiss(DialogInterface dialog) { 


     } 
    }); 
    alert.show(); 

佈局XML文件是:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"> 

<ImageView 
    android:src="@drawable/yo3" 
    android:layout_width="match_parent" 
    android:layout_height="64dp" 
    android:scaleType="center" 
    android:background="#FFFFBB33" 
    android:contentDescription="@string/app_name" /> 

<TextView 
    android:id="@+id/dialogtext" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    /> 

</LinearLayout>` 
+0

你爲什麼不加入佈局文件對話框文本? – Confuse 2014-10-21 04:10:20

回答

2

不要設置消息,你做的方式。

在您的自定義佈局中,您必須設置的textview是「dialogtext」TextView。 試試看...我看到我膨脹的自定義視圖,並從該視圖中獲得自定義對話框(您之前未實際設置),並將該消息設置爲在構建發生後顯示。 其實你可以有任何自定義視圖你想和你喜歡,甚至把手上的事件設置視圖中的每個元素

AlertDialog.Builder builder = new AlertDialog.Builder(this); 
LayoutInflater inflater = getLayoutInflater(); 

View customView = inflater.inflate(R.layout.custom_dialog, null); 
TextView messageView = (TextView)customView.findViewById(R.id.dialogtext); 


builder.setView(customView) 
     .setPositiveButton("Share", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 

       if (isNetworkAvailable()){ 

        if (......; 

        } else { 
         ...... 
        } 

       } else { 
        .......; 
       }  
      } 
     }) 
     .setNegativeButton("Shake again!", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       // User cancelled the dialog 

      } 

    }).setOnCancelListener(new OnCancelListener() { 

     @Override 
     public void onCancel(DialogInterface dialog) { 

     } 
    }); 

messageView.SetText("Message" + message); 



AlertDialog alert = builder.create(); 

alert.setOnDismissListener(new OnDismissListener() { 

    @Override 
    public void onDismiss(DialogInterface dialog) { 


    } 
}); 
alert.show(); 
+0

感謝gispyros,解決了它。 – user3303848 2014-10-21 06:54:50