...我已經尋找的答案,不發現它,所以請幫我:)自定義對話框返回布爾值
我有一個自定義類:
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,因爲返回真,假值不要在自定義類的工作,該對話框不會在用戶點擊之前等待,它自動返回一個值。
簡單的好友,而不是創建對話框的自定義類使用'應用程序'類定義相同的方法,但方法'setDialog()'返回'Void'和聲明變量'布爾值'全球範圍內,現在你可以在任何地方得到用戶選擇的內容 – user370305
關於Application類的最好的事情是,當您想要顯示Dialog時,您不需要傳遞當前活動的上下文。 – user370305
您無法同步獲取返回值。你將不得不使用一個監聽器 – nandeesh