简体   繁体   English

Android关闭警报对话框和活动一起

[英]Android Close Alert Dialog box and Activity together

In my android application I have an alertdialog box will open as sson as the activity is created and based on the text given in that alertdialog search is done and result is displayed in a listview. 在我的android应用程序中,我将创建一个活动对话框,并在创建活动时以sson的形式打开一个alertdialog框,并基于该对话框中给出的文本进行搜索并在listview中显示结果。 But when I am clicking the backbutton, alert dialog close first and then again I need to click back button to close the current activity. 但是,当我单击后退按钮时,警报对话框首先关闭,然后再次关闭,我需要单击后退按钮以关闭当前活动。 But I need to close both alert dialog and Activity in one back button click.I am giving the code below. 但是我需要单击一次后退按钮来关闭警报对话框和活动。我在下面提供代码。

AlertDialog alertDialog;

    public SampleTest() {
        alertDialog = null;
    }


    public void onCreate(Bundle savedInstanceState)

    {

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



        LayoutInflater li = LayoutInflater.from(context);
        View promptsView = li.inflate(R.layout.search_branch_predict, null);

        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                context);

        // set prompts.xml to alertdialog builder
        alertDialogBuilder.setView(promptsView);

        final EditText userInput = (EditText) promptsView
                .findViewById(R.id.edittext);

        final Button btnSearch = (Button) promptsView.findViewById(R.id.search_Button);


        // create alert dialog
        alertDialog = alertDialogBuilder.create();

        // show it
        alertDialog.show();

}
}
}

Override onBackPressed() in your activity and check for is your alert dialog box is open then called finish to kill activity. 在您的活动中覆盖onBackPressed()并检查是否打开了警报对话框,然后调用“完成”以终止活动。

    @Override
   public void onBackPressed() {
    if(alertDialog.isShowing()){
     alertDilaog.dismiss(); // this will dismiss your dialog
     finish(); // this will kill your activity
    }else{
     super.onBackPressed();   
    }
  }

Try this, 尝试这个,

@Override  
public boolean onKeyDown(int keyCode, KeyEvent event)  
{  
     //replaces the default 'Back' button action  
     if(keyCode==KeyEvent.KEYCODE_BACK)  
     {  

        dialog.dismiss();
        finish();

     }  
     return true;  
}  

or 要么

@Override
public void onBackPressed() {
    dialog.dismiss();
    finish();
}

Edit 1: 编辑1:

LayoutInflater li = LayoutInflater.from(context);
LinearLayout mainLayout = (LinearLayout) li.inflate(R.id.YOUR_LAYOU_ID_HERE, this);

mainLayout.setOnTouchListener(new OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {
            int eid = event.getAction();
            switch (eid) {
            case MotionEvent.ACTION_DOWN:
                finish();
                break;
            }
            return true;
        }
    });

This may helps you. 这可能对您有帮助。

You can override onBackPressed() method as: 您可以按以下方式覆盖onBackPressed()方法:

 @Override
    public void onBackPressed() {
        alertDialog.dismiss();
        yourActivity.this.finish();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM