在Activity关闭时正确设置alertdialog?

时间:2015-04-21 20:00:59

标签: android android-activity alertdialog

我的活动有两个得分号码。我希望用户在关闭时(例如,玩家1或玩家2)选择赢家。作为一个.NET人,我试图基本上做MessageBox。

不幸的是,从我所读过的内容来看,android并没有以同样的方式做UI线程。并且活动有一个泄漏的窗口"。我看着SO,我看到的想法是放弃"解雇" in" onPause"。但是,我没有看到" onPause"功能,并对如何实现它感到困惑。所以我尝试了下面的内容,但得到了同样的错误。

为了确保这项工作正常,我该怎么办?

  public void onFinish(View v) {
    //get scores
    TextView score1tv = (TextView) findViewById(R.id.tvScore1);
     score1 = score1tv.getText().toString();
    TextView score2tv = (TextView) findViewById(R.id.tvScore2);
     score2 = score2tv.getText().toString();

    //Make sure they aren't the same
    int i_Score1 = Integer.parseInt(score1);
    int i_Score2 = Integer.parseInt(score2);

    if (i_Score1 == i_Score2){

      //alert user
      new AlertDialog.Builder(this)
                .setTitle("Duplicate score")
                .setMessage("Whose score won the bout?")
                .setPositiveButton(boutData[1], new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        //fencer 1 won
                        score1 = "V" + score1;
                        dialog.dismiss();
                    }
                })
                .setNegativeButton(boutData[3], new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        score2 = "V" + score2;
                        dialog.dismiss();
                    }
                })
                .setIcon(android.R.drawable.ic_dialog_alert)
                .create()
                .show();
    }
    Intent intent = new Intent();
    intent.putExtra("edittextvalue","value_here");
    setResult(RESULT_OK, intent);
    finish();
}

修改:由于我收到的帮助,我找到了解决方案:

 public void onFinish(View v) {
    //get scores
    TextView score1tv = (TextView) findViewById(R.id.tvScore1);
     score1 = score1tv.getText().toString();
    TextView score2tv = (TextView) findViewById(R.id.tvScore2);
     score2 = score2tv.getText().toString();

    //Make sure they aren't the same
    int i_Score1 = Integer.parseInt(score1);
    int i_Score2 = Integer.parseInt(score2);

    if (score1 == score2){

      //alert user
      new AlertDialog.Builder(this)
                .setTitle("Duplicate score")
                .setMessage("Whose score won the bout?")
                .setPositiveButton(boutData[1], new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        //fencer 1 won
                        score1 = "V" + score1;

                        //close out
                        Intent intent = new Intent();
                        intent.putExtra("edittextvalue","value_here");
                        setResult(RESULT_OK, intent);
                        finish();
                       }
                })
                .setNegativeButton(boutData[3], new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        score2 = "V" + score2;

                        //close out
                        Intent intent = new Intent();
                        intent.putExtra("edittextvalue","value_here");
                        setResult(RESULT_OK, intent);
                        finish();

                    }
                })
                .setIcon(android.R.drawable.ic_dialog_alert)
                .create()
                .show();
    }
    else{
        Intent intent = new Intent();
        intent.putExtra("edittextvalue","value_here");
        setResult(RESULT_OK, intent);
        finish();
    }

}

1 个答案:

答案 0 :(得分:1)

将最后四行放入else子句并在onClick方法中执行类似操作。

此外,您不必手动调用dialog.dismiss()。