2012-03-14 98 views

回答

7

試試這個:

final Dialog commentDialog = new Dialog(this); 
commentDialog.setContentView(R.layout.reply); 
Button okBtn = (Button) commentDialog.findViewById(R.id.ok); 
okBtn.setOnClickListener(new View.OnClickListener() { 

          @Override 
          public void onClick(View v) { 
            //do anything you want here before close the dialog 
            commentDialog.dismiss(); 
          } 
}); 
Button cancelBtn = (Button) commentDialog.findViewById(R.id.cancel); 
cancelBtn.setOnClickListener(new View.OnClickListener() { 

          @Override 
          public void onClick(View v) { 

            commentDialog.dismiss(); 
          } 
}); 

reply.xml

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

    <EditText android:layout_width="fill_parent" 
      android:layout_gravity="center" android:layout_height="wrap_content" 
      android:hint="@string/your_comment" android:id="@+id/body" android:textColor="#000000" 
      android:lines="3" /> 
    <LinearLayout android:layout_width="fill_parent" 
      android:layout_height="fill_parent"> 
      <Button android:id="@+id/ok" android:layout_height="wrap_content" 
        android:layout_width="wrap_content" android:text="@string/send" android:layout_weight="1" 
        /> 
      <Button android:id="@+id/cancel" android:layout_height="wrap_content" 
        android:layout_width="wrap_content" android:layout_weight="1" 
        android:text="@android:string/cancel" /> 
    </LinearLayout> 
</LinearLayout> 
+4

添加這個來顯示對話框。 commentDialog.show(); – Sahil 2015-10-10 15:42:56

+0

爲什麼它不共享其餘的樣式? – FtheBuilder 2016-05-26 03:26:26

2

您可以使用XML佈局創建自定義對話框。並且,如果要阻止它,直到用戶輸入文本並點擊確定按鈕,您可以使其成爲setCancelable(false)

使用谷歌的搜索鏈接:Dialog With EditText

+0

setCancelable(false)只是膚淺的?我的意思是它真的可以暫停一切嗎?在我的應用程序中,我希望收集數據的活動位於要暫停的對話框後面。 – 2013-06-20 07:46:30

1

我想你的意思是一個模態對話框。

我會說看看AlertDialog.Builder作爲一個開始,更新的方法是使用DialogFragment,它可以讓您更好地控制對話框的生命週期。

你正在尋找的關鍵方法是dialog.setCancelable(false);

+0

setCancelable(false)只是膚淺的?我的意思是它真的可以暫停一切嗎?在我的應用程序中,我希望收集數據的活動位於要暫停的對話框後面。即停止數據收集直到用戶關閉對話。它會實現嗎? – 2013-06-20 07:47:36

+0

@perfectionming我認爲你需要開一個新的問題。 – 2013-06-20 10:27:17

+1

@ perfectionm1ng你需要用自己的代碼暫停「一切」。因爲這個模式對話框不能爲你做。也許你可以在調用show()對話框之前暫停數據收集。 – 2013-08-06 21:05:00

相關問題