2012-09-04 38 views
2

...我已經尋找的答案,不發現它,所以請幫我:)自定義對話框返回布爾值

我有一個自定義類:

public class CustomClass { 

    private final Context ctx; 
    public CustomClass(Context ctx) { 
     this.ctx = ctx; 
    } 

    public boolean setDialog(int head, int text) { 
     final boolean value; 

     final Dialog d = new Dialog(ctx); 
     d.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     d.setContentView(R.layout.custom2_dialog); 

     TextView txtHead = (TextView) d.findViewById(R.id.custom2_txtHead); 
     txtHead.setText(ctx.getResources().getString(head)); 
     TextView txtText = (TextView) d.findViewById(R.id.custom2_txtText); 
     txtText.setText(ctx.getResources().getString(text)); 

     Button btnOK = (Button) d.findViewById(R.id.custom2_btnOK); 
     btnOK.setText("OK"); 
     btnOK.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View arg0) { 
       value = true; 
       d.dismiss(); 
      } 
     }); 

     Button btnNO = (Button) d.findViewById(R.id.custom2_btnNO); 
     btnNO.setText("NO"); 
     btnNO.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View arg0) { 
       value = false; 
       d.dismiss(); 
      } 
     }); 

     d.show(); 

     return value; 
    } 
} 

你可以看到我在我的自定義類中創建了一個自定義對話框,因爲我不想在每個活動中創建一個對話框。現在,當我使用它在一個活動:

CustomClass cC = new CustomClass(this); 
if(cC.setDialog(R.string.head, R.string.text)) { 
    // user checked OK 
} else { 
    // user checked NO 
} 

如何知道如果用戶檢查OK NO,因爲返回真,假值不要在自定義類的工作,該對話框不會在用戶點擊之前等待,它自動返回一個值。

+1

簡單的好友,而不是創建對話框的自定義類使用'應用程序'類定義相同的方法,但方法'setDialog()'返回'Void'和聲明變量'布爾值'全球範圍內,現在你可以在任何地方得到用戶選擇的內容 – user370305

+0

關於Application類的最好的事情是,當您想要顯示Dialog時,您不需要傳遞當前活動的上下文。 – user370305

+0

您無法同步獲取返回值。你將不得不使用一個監聽器 – nandeesh

回答

0

這是您的問題的解決方案。首先,如果您正在創建「自定義」對話框,則必須擴展類以讀取響應並實施OnClickListener

class CustomizeDialog extends Dialog implements OnClickListener 
{ 
    // some code 
public CustomizeDialog(Activity activity, String title, String msg, int i, Typeface typeface) 
    { 
     super(activity);   
     this.activity = activity; 
     intFlag = i; 
     setContentView(R.layout.relative); 
     dialogButtonYes = (Button) findViewById(R.id.buttonCustomDialogYes); 
     dialogButtonNo = (Button) findViewById(R.id.buttonCustomDialogNo); 
     textView = (TextView) findViewById(R.id.textViewCustomTitle); 
     this.msg = msg; 
     dialogButtonNo.setOnClickListener(this); 
     dialogButtonYes.setOnClickListener(this); 
    } 
} 

@Override 
public void onClick(View v) { 
     if (v == dialogButtonYes) 
     { 
      Intent intent =new Intent(activity, CallForOne.class); 
      activity.startActivity(intent); 
      dismiss(); 
     } 
     else 
      dismiss(); 
    } 
    else if(intFlag == 2) 
    { 
     if (v == dialogButtonYes) 
     { 
      Intent intent =new Intent(activity, CallMe.class); 
      activity.startActivity(intent); 
      dismiss(); 
     } 
     else 
      dismiss(); 
} 

而且relative.xml文件

<TextView 
    android:id="@+id/textViewCustomTitle" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="10dp" /> 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/textViewCustomTitle" 
    android:orientation="horizontal" 
    android:paddingBottom="1.0dip" 
    android:paddingLeft="4.0dip" 
    android:paddingRight="4.0dip" 
    android:paddingTop="5.0dip" > 

    <Button 
     android:id="@+id/buttonCustomDialogYes" 
     style="@style/styleNormalButton" 
     android:layout_width="0.0dp" 
     android:layout_height="fill_parent" 
     android:layout_weight="1.0" 
     android:text="@string/yes" /> 

    <Button 
     android:id="@+id/buttonCustomDialogNo" 
     style="@style/styleNormalButton" 
     android:layout_width="0.0dp" 
     android:layout_height="fill_parent" 
     android:layout_weight="1.0" 
     android:text="@string/no" /> 
</LinearLayout> 

和主類

class Main implements OnClickListener 
    { 
    onCreate() 
    { 
    //your code 
    } 

    public void on click() 
    { 
    customizeDialog = new CustomizeDialog(CustomDialogExample.this,getString(R.string.title_for_one),getString(R.string.msg_for_one),1,"String"); 
    customizeDialog.show(); 
    } 
} 
+0

有沒有其他解決方案?,我不想爲此創建另一個課程。 – Gabrijel

0

首先來自@ user370305的評論是正確的做法。可以有其他方式毫無疑問。

你所面對的問題是,因爲當你調用自定義類

if(cC.setDialog(R.string.head, R.string.text)); 

你得到,即使沒有被點擊

return value; 

假值的方法,這是當您聲明時設置的默認值爲

final boolean value; 

取而代之的是你ld聽取消和確定點擊

+0

你能舉個例子嗎?我認爲有一個簡單的解決方案。 – Gabrijel