警报对话框关闭而不按任何thing_android

时间:2014-04-23 20:39:42

标签: android android-alertdialog

我想要razrada.show()下面的代码;按我的AlertDialog上的Save后执行,但它显示并关闭 这是我的代码

AlertDialog.Builder razradaPlacanja = new AlertDialog.Builder(NoviRacun.this);
            razradaPlacanja.setTitle("Način plaćanja");
            LayoutInflater inflater = getLayoutInflater();
            View vieww = inflater.inflate(R.layout.razrada, null);
            razradaPlacanja.setView(vieww);
final EditText gotovinaEdit = (EditText) vieww.findViewById(R.id.gotovinaEdit);
final EditText karticeEdit = (EditText) vieww.findViewById(R.id.karticeEdit);
razradaPlacanja.setPositiveButton("Save",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            try {
                                json.put("TotalCash", gotovinaEdit.getText().toString());
                                json.put("TotalCreditCards", karticeEdit.getText().toString());

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }
                    });


            AlertDialog razrada = razradaPlacanja.create();
            razrada.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
            razrada.show();
            //this i want to execute after pressing Save 
            Racuni racun = getInsertResponse(requestInsert(base64EncodedCredentials, json, httpclient1));
            try {
...

我不能放     Racuni racun = getInsertResponse(requestInsert(base64EncodedCredentials,json,httpclient1)); 在onClick块内导致然后racun无法访问(如果在外面声明,则需要是final,然后我不能asign值)

感谢您提供任何帮助!

1 个答案:

答案 0 :(得分:0)

您可以将要执行的代码移动到单独的方法中:

public void someAction(JsonType json);
{
    Racuni racun = getInsertResponse(requestInsert(base64EncodedCredentials, json, httpclient1));
    // the rest of the code that uses racun, etc.
}

然后从对话框的onClick()方法中调用它:

YourActivity.this.someAction(json);

请注意,show() AlertDialog之后的任何代码都会立即执行。 show()并没有阻止流程。