对话框中,点击按钮的正后仍显示 [英] Dialog still show after click positive button

查看:149
本文介绍了对话框中,点击按钮的正后仍显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对单击显示自定义对话框时,密码的EditText和OK按钮,取消按钮,如果你把正确的密码,它打开另一个活动(文本活动)(CustomDilaog活动)按钮,直至现在每天的事情做工精细,

我有两个问题的部分。

第一部分::当即时通讯在(文本活性)和preSS后退按钮在它返回到(CustomDilaog活动),仍然是对话节目,如何让它解散。

第二部分:对话框解雇后,如果我不写密码,只需点击带的EditText空它没有任何反应OK按钮,如何让这个点击刚刚解雇对话框,而不采取任何行动(这是开放(文活动)如果写了正确的密码。

(CustomDilaog活动):

 公共类CustomDilaog延伸活动{最后上下文的背景下=这;
私人Button按钮;公共无效的onCreate(捆绑savedInstanceState){    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);    TextView的电视=(的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)));    按钮=(按钮)findViewById(R.id.button1);
    //添加按钮监听器
    button.setOnClickListener(新OnClickListener(){
    公共无效的onClick(查看为arg0){        //定制对话框
        最后对话的对话=新的对话框(背景下,R.style.cust_dialog);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.custom);        //设置自定义对话框组件 - 文字,图片和按钮
        TextView的文本=(TextView的)dialog.findViewById(R.id.text);
        字体的字体= Typeface.createFromAsset(getAssets(),BFantezy.ttf);
        text.setTypeface(字体);
        text.setText(写入密码:);        按钮dialogBu​​tton =(按钮)dialog.findViewById(R.id.dialogBu​​ttonOK);
        字体font1 = Typeface.createFromAsset(getAssets(),BFantezy.ttf);
        dialogBu​​tton.setTypeface(font1);
        //如果点击按钮,关闭对话框定制
        dialogBu​​tton.setOnClickListener(新OnClickListener(){
            公共无效的onClick(视图v){
                的EditText密码=(EditText上)dialog.findViewById(R.id.password);
                ////
                如果(。password.getText()的toString()长度()大于0){
                    如果(password.getText()。的toString()。等于(测试)){
                意向意图=新意图(CustomDilaog.this,Text.class);
                startActivity(意向);               }
                    其他{
                        //得到你custom_toast.xml布局
                        LayoutInflater吹气= getLayoutInflater();                        查看布局= inflater.inflate(R.layout.custom_toast,
                                (ViewGroup中)findViewById(R.id.custom_toast));                        //设置一个虚拟形象
                        ImageView的图像=(ImageView的)layout.findViewById(R.id.image_toast);
                        image.setImageResource(R.drawable.ic_launcher);                        //设置消息
                        TextView的文本=(TextView的)layout.findViewById(R.id.text_toast);
                        text.setText(密码错误);                        //吐司...
                        吐司面包=新吐司(getApplicationContext());
                        toast.setGravity(Gravity.CENTER_VERTICAL,0,0);
                        toast.setDuration(Toast.LENGTH_LONG);
                        toast.setView(布局);
                        toast.show();                        }
                    }
            }
        });        按钮dialogBu​​ttonCancell =(按钮)dialog.findViewById(R.id.cancel);
        字体font11 = Typeface.createFromAsset(getAssets(),BFantezy.ttf);
        dialogBu​​ttonCancell.setTypeface(font11);        dialogBu​​ttonCancell.setOnClickListener(新OnClickListener(){
               公共无效的onClick(视图v){
               dialog.dismiss();
               }
            });        dialog.show();
      }
    });
}
}


解决方案

通话dialog.dismiss()在dialogBu​​tton点击听众的开头:

  dialogBu​​tton.setOnClickListener(新OnClickListener(){
   公共无效的onClick(视图v){      dialog.dismiss();      的EditText密码=(EditText上)dialog.findViewById(R.id.password);
      如果(。password.getText()的toString()长度()大于0){      ...
   }
});

I have button on (CustomDilaog activity) when clicked show custom dialog with password edittext and OK button and cancel button , if you put correct password it open another activity (Text activity), till now every things work fine ,

i have question with two parts.

Part one : when im in (Text activity) and press back button to return to (CustomDilaog activity) , still the dialog show over it , how to let it dismiss

Part two : after dialog fired , if i dont write password and just click OK button with edittext empty it has no response , how to let this click just dismiss dialog without no action ( which is open (Text activity) if wrote correct password .

(CustomDilaog activity):

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();
      }
    });
}
}

解决方案

call dialog.dismiss() at the beginning of dialogButton click listener:

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 ) {

      ...
   }
});

这篇关于对话框中,点击按钮的正后仍显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆