【问题标题】:Dialog still show after click positive button单击正按钮后对话框仍然显示
【发布时间】:2013-09-18 17:41:33
【问题描述】:

当点击显示自定义对话框时,我有按钮(CustomDilaog 活动),带有密码编辑文本和确定按钮和取消按钮,如果你输入正确的密码,它会打开另一个活动(文本活动),直到现在一切正常,

我对两个部分有疑问。

第一部分:当我在(文本活动)并按返回按钮返回(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();
      }
    });
}
}

【问题讨论】:

    标签: android dialog


    【解决方案1】:

    在dialogBu​​tton点击监听的开头调用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 ) {
    
          ...
       }
    });
    

    【讨论】:

      【解决方案2】:

      第一部分:当我在(文本活动)并按返回按钮返回(CustomDilaog活动)时,对话框仍然显示在它上面,如何让它关闭

      当您启动新的Activity 时,只需在您的CustomDialogActivity 中调用finish()。看起来你的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()

      【讨论】:

      • 关于第一部分:如果我在意图后调用finish(),用后退按钮按下它会退出文本活动,但我希望它返回到Customdialog活动而不看到你的对话框仍然出现,谢谢
      • 我错过了什么?你有一个Activity (CustomDialog) 和一个Dialog,它以相同的Activity 开头。
      • 对不起,这只是我调用的活动名称混淆,Customdialogactivity 包含许多视图 3 个文本和按钮,单击时会触发带有 edittext 密码的自定义对话框,如果您输入正确的密码并单击确定它打开文字活动,我现在清楚了吗,谢谢
      • 我仍然很困惑,但我的答案的第二部分不是你需要的吗?
      • 我现在在外面写frm mobile,让我回家然后我会更好地解释并检查第二部分,谢谢
      猜你喜欢
      • 1970-01-01
      • 2013-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多