Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android关闭警报对话框和活动在一起_Android_Android Activity_Android Alertdialog - Fatal编程技术网

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

Android关闭警报对话框和活动在一起,android,android-activity,android-alertdialog,Android,Android Activity,Android Alertdialog,在我的android应用程序中,我有一个alertdialog对话框,在创建活动时,它将以sson的形式打开,并根据alertdialog中给出的文本进行搜索,结果显示在列表视图中。但当我单击backbutton时,首先关闭警报对话框,然后再次单击back按钮关闭当前活动。但我需要关闭警报对话框和活动在一个返回按钮点击。我给下面的代码 AlertDialog alertDialog; public SampleTest() { alertDialog = null;

在我的android应用程序中,我有一个alertdialog对话框,在创建活动时,它将以sson的形式打开,并根据alertdialog中给出的文本进行搜索,结果显示在列表视图中。但当我单击backbutton时,首先关闭警报对话框,然后再次单击back按钮关闭当前活动。但我需要关闭警报对话框和活动在一个返回按钮点击。我给下面的代码

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

}
}
}

您可以将onBackPressed()方法重写为:

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

您可以将onBackPressed()方法重写为:

 @Override
    public void onBackPressed() {
        alertDialog.dismiss();
        yourActivity.this.finish();
    }
在“活动”中重写onBackPressed(),并检查“警报”对话框是否打开,然后调用“完成终止活动”

    @Override
   public void onBackPressed() {
    if(alertDialog.isShowing()){
     alertDilaog.dismiss(); // this will dismiss your dialog
     finish(); // this will kill your activity
    }else{
     super.onBackPressed();   
    }
  }
在“活动”中重写onBackPressed(),并检查“警报”对话框是否打开,然后调用“完成终止活动”

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

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

        dialog.dismiss();
        finish();

     }  
     return true;  
}  

编辑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;
        }
    });
这可能对你有帮助。

试试这个

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

        dialog.dismiss();
        finish();

     }  
     return true;  
}  

编辑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;
        }
    });

这可能会对您有所帮助。

如果您在
OnBackPressed()
中调用
finish()
,则显示OnBackPressed代码。如果您在
OnBackPressed()
中调用
finish()
则显示OnBackPressed代码,则将完成活动。这里有一个布局图和视图。。我想我需要先关闭视图,再关闭活动是的,试着先关闭视图,再关闭活动。你知道怎么做吗?一个布局平面和视图就在那里。。我想我需要先关闭视图,然后再关闭活动是的,试着先关闭视图,然后再关闭活动。你知道怎么做吗