2013-02-26 8 views
1

사용자 지정 대화 상자를 만들어 하나의 표준 대화 상자를 만들어 나중에 한 줄만 만들면 표준으로 사용할 수 있습니다. 매개 변수를 사용하여 텍스트를 변경합니다. 대화 상자는 매우 단순하며 하나의 단추 만 있습니다.사용자 지정 대화 상자가 응답 대기

이제이 문제에 대해 다시 생각해 보니 좋은 생각이었습니다. 실제로 대화 상자가 표시 될 때까지 내 앱이 멈추길 원합니다. 어떻게 관리 할 수 ​​있니? 대화 상자에 반환 유형을 지정 하시겠습니까? 아니면 더 좋은 방법이 있습니까?

내 대화 :

/** 
* custom dialog 
* 
* @param mcontext use activityname.this 
* @param title 
* @param text 
* @param button 
*/ 
public void showDialog(Context mcontext, String title,String text, String button) { 
    // fonts 
    Typeface tf_hn = Typeface.createFromAsset(mcontext.getAssets(), "helveticaneue.ttf"); 
    Typeface tf_hn_bold = Typeface.createFromAsset(mcontext.getAssets(), "helveticaneuebd.ttf"); 
    Resources res = mcontext.getResources(); 

    // custom dialog 
    final Dialog dialog = new Dialog(mcontext); 
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); //not the normal dialog title 
    dialog.setContentView(R.layout.view_dialog); 

    TextView tv_dialog_title = (TextView) dialog.findViewById(R.id.tv_dialog_title); 
    tv_dialog_title.setText(title); 
    tv_dialog_title.setTypeface(tf_hn_bold); 
    tv_dialog_title.setTextColor(res.getColor(R.color.white)); 

    TextView tv_dialog_text = (TextView) dialog.findViewById(R.id.tv_dialog_text); 
    tv_dialog_text.setText(text); 
    tv_dialog_text.setTypeface(tf_hn); 
    tv_dialog_text.setTextColor(res.getColor(R.color.white)); 

    Button dialogButton = (Button) dialog.findViewById(R.id.bt_dialog_button); 
    dialogButton.setTypeface(tf_hn_bold); 
    dialogButton.setText(button); 
    dialogButton.setTextColor(res.getColor(R.color.white)); 
    // if button is clicked, close the custom dialog 
    dialogButton.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      dialog.dismiss(); 
     } 
    }); 

    dialog.show(); 
} 

하고 난 다음과 같이 사용할 수 있습니다 :

내가 "당신이 로그인"이있는 대화 상자를 보여주고 싶었다 때까지, 모든 괜찮 았는데
dialogH.showDialog(LoginActivity.this, res.getString(R.string.txt_dialog_fout), res.getString(R.string.txt_dialog_not_connected),res.getString(R.string.txt_dialog_button)); 

(또는 디스플레이를 클릭 한 후 인 텐트를 시작하십시오. 누구 아이디어?

답변

3

이렇게 내장 된 수신기를 사용하여 사용자 지정 대화 상자 클래스를 만듭니다. 당신을 맞는 경우도 생성자의 수신기를 포함 할 수

MyDialog dialog = new MyDialog(getContext(), title, text, button); 
    dialog.setDialogListener(new MyDialog.DialogListener() { 

     @Override 
     public void onCompleted() { 
      // do stuff when dialog is completed 
     } 

     @Override 
     public void onCanceled() { 
      // do stuff when dialog is cancelled 
     } 
    }); 
    dialog.show(); 
+0

:

public class MyDialog extends Dialog { String title; String text; String button; DialogListener listener; interface DialogListener { void onCompleted(); void onCanceled(); } public MyDialog(Context context, String title, String text, String button) { super(context); this.title = title; this.text = text; this.button = button; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Typeface tf_hn = Typeface.createFromAsset(getContext().getAssets(), "helveticaneue.ttf"); Typeface tf_hn_bold = Typeface.createFromAsset(getContext()..getAssets(), "helveticaneuebd.ttf"); Resources res = getContext().getResources(); requestWindowFeature(Window.FEATURE_NO_TITLE); // not the normal dialog title setContentView(R.layout.view_dialog); TextView tv_dialog_title = (TextView) findViewById(R.id.tv_dialog_title); tv_dialog_title.setText(title); tv_dialog_title.setTypeface(tf_hn_bold); tv_dialog_title.setTextColor(res.getColor(R.color.white)); TextView tv_dialog_text = (TextView) findViewById(R.id.tv_dialog_text); tv_dialog_text.setText(text); tv_dialog_text.setTypeface(tf_hn); tv_dialog_text.setTextColor(res.getColor(R.color.white)); Button dialogButton = (Button) findViewById(R.id.bt_dialog_button); dialogButton.setTypeface(tf_hn_bold); dialogButton.setText(button); dialogButton.setTextColor(res.getColor(R.color.white)); // if button is clicked, close the custom dialog dialogButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(listener != null) listener.onCompleted(); MyDialog.this.dismiss(); } }); setOnCancelListener(new OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { if(listener != null) listener.onCanceled(); } }); } public void setDialogListener(DialogListener listener) { this.listener = listener; } } 

그리고

대화 상자를 구현하는 – invertigo