单击肯定按钮后仍显示对话框

时间:2013-09-18 17:41:33

标签: android dialog

我点击按钮(CustomDilaog活动),点击显示自定义对话框,密码edittext和OK按钮和取消按钮,如果你输入正确的密码,它打开另一个活动(文本活动),到现在为止每件事情都正常,

我有两个部分的问题。

第一部分:当我进入(文字活动)并按回按钮返回(CustomDilaog活动)时,仍然显示对话框,如何让它解雇

第二部分:对话框触发后,如果我不写密码,只需单击OK按钮,其中edittext为空它没有响应,如何让这个单击只是解除对话框而不做任何动作(这是打开的) (文字活动)如果写了正确的密码。

(CustomDilaog活动):

public class CustomDilaog extends Activity {

final Context context = this;
private Button button;

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    TextView tv=(TextView)findViewById(R.id.introclusion_tv1);
    tv.setTypeface(FontFactory.getBFantezy(getBaseContext()));

    TextView tv1=(TextView)findViewById(R.id.introclusion_tv2);
    tv1.setTypeface(FontFactory.getBFantezy(getBaseContext()));
    tv1.setText(Html.fromHtml(getString(R.string.introclusion)));

    button = (Button) findViewById(R.id.button1); 
    // add button listener
    button.setOnClickListener(new OnClickListener() {
    public void onClick(View arg0) {

        // custom dialog
        final Dialog dialog = new Dialog(context,R.style.cust_dialog);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);           
        dialog.setContentView(R.layout.custom);

        // set the custom dialog components - text, image and button
        TextView text = (TextView) dialog.findViewById(R.id.text);
        Typeface font = Typeface.createFromAsset(getAssets(), "BFantezy.ttf"); 
        text.setTypeface(font);
        text.setText("write password :");

        Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
        Typeface font1 = Typeface.createFromAsset(getAssets(), "BFantezy.ttf"); 
        dialogButton.setTypeface(font1);
        // if button is clicked, close the custom dialog
        dialogButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                EditText password = (EditText) dialog.findViewById(R.id.password);
                ////
                if( password.getText().toString().length() > 0 ) {
                    if( password.getText().toString().equals("test")) {
                Intent intent = new Intent(CustomDilaog.this,Text.class);
                startActivity(intent);

               }
                    else{
                        // get your custom_toast.xml layout
                        LayoutInflater inflater = getLayoutInflater();

                        View layout = inflater.inflate(R.layout.custom_toast,
                                (ViewGroup) findViewById(R.id.custom_toast));

                        // set a dummy image
                        ImageView image = (ImageView) layout.findViewById(R.id.image_toast);
                        image.setImageResource(R.drawable.ic_launcher);

                        // set a message
                        TextView text = (TextView) layout.findViewById(R.id.text_toast);
                        text.setText("Wrong password");

                        // Toast...
                        Toast toast = new Toast(getApplicationContext());
                        toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
                        toast.setDuration(Toast.LENGTH_LONG);
                        toast.setView(layout);
                        toast.show();

                        }
                    }                   
            }
        });

        Button dialogButtonCancell = (Button) dialog.findViewById(R.id.cancel);
        Typeface font11 = Typeface.createFromAsset(getAssets(), "BFantezy.ttf"); 
        dialogButtonCancell.setTypeface(font11);

        dialogButtonCancell.setOnClickListener(new OnClickListener() {
               public void onClick(View v) {                            
               dialog.dismiss();                        
               }
            });

        dialog.show();
      }
    });
}
}

2 个答案:

答案 0 :(得分:1)

在dialogBu​​tton click listener:

的开头调用dialog.dismiss()
dialogButton.setOnClickListener(new OnClickListener() {
   public void onClick(View v) {

      dialog.dismiss();

      EditText password = (EditText) dialog.findViewById(R.id.password);
      if( password.getText().toString().length() > 0 ) {

      ...
   }
});

答案 1 :(得分:0)

  

第一部分:当我进入(文本活动)并按回按钮返回(CustomDilaog活动)时,仍然显示对话框,如何让它解雇

当您启动新的finish()时,只需在CustomDialogActivity中致电Activity即可。看起来你的Intent正在调用同样的Activity,所以我对此感到有些困惑。

if( password.getText().toString().length() > 0 ) {
       if( password.getText().toString().equals("test")) {
            Intent intent = new Intent(CustomDilaog.this,Text.class);
            startActivity(intent);
  

第二部分:在对话框触发后,如果我没有写密码而只是单击OK按钮,其中edittext为空它没有响应,如何让这个单击只是忽略对话而不是没有动作(如果写的正确则打开(文本活动)密码。

向您的第一个else添加if语句,并在其中添加dialog.dismiss()

相关问题