android - 如何在出现警报对话框后禁用背景屏幕或仅启用警报对话框?

标签 android android-alertdialog

游戏结束后我有一个游戏,我使用警报对话框将分数和名称保存到数据库中,这很好但是当我单击游戏窗口上的任何位置时警报对话框关闭并且游戏再次运行我需要在游戏后禁用游戏窗口结束或警报对话框从未关闭事件我点击游戏窗口。

我的代码是:

mainactivity:

public class MainActivity extends Activity {

static TextView editText;
static int score=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);     
     editText=(TextView) findViewById(R.id.editText1);
     //editText.setText(""+score);
     Button button=(Button) findViewById(R.id.button1);
     button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            new AnimatedView(MainActivity.this).pauseClicked();
        }
    });
}
public static void setCount(int count) {

    //new MainActivity().onCreate(null);
    score=count;
    editText.setText(""+score);
}

动画 View :

public class AnimatedView extends ImageView{

String name;
static int count=0;
private Context mContext;
int x = 130;
int y = 450;
private float a,b;
private int xVelocity = 20;
private int yVelocity = 20;
private Handler h;
private final int FRAME_RATE = 25;
BitmapDrawable ball;
boolean touching;
boolean dm_touched = false;
float move=3;
int bm_x = 0, bm_y = 0, bm_offsetx, bm_offsety,bm_w,bm_h;
boolean paused;
private Paint line, ball1, background;
static int click=0;
private static final int TEXT_ID = 0;
DBCeation dbCeation;
public AnimatedView(Context context) {
    super(context);
}
public AnimatedView(Context context, AttributeSet attrs)  {  
    super(context, attrs);  
    mContext = context; 
    h = new Handler();
} 

private Runnable r = new Runnable() {
    @Override
    public void run() {
        //Log.e("game","run called");
        if(touching = true)
        invalidate(); 
    }
};


@Override
protected void onDraw(Canvas c) {  
    BitmapDrawable ball = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.ball);  
    if (x<0 && y <0) {
        //x = this.getWidth()/2;
        y = c.getHeight()/2;


    } else {
        //Log.d("s",""+xVelocity);
        Log.d("s",""+yVelocity);
        x += xVelocity;
        y += yVelocity;
        if ((x > this.getWidth() - ball.getBitmap().getWidth()) || (x < 0)) {
            xVelocity = xVelocity*-1;
        }
        if (y >( this.getHeight() - ball.getBitmap().getHeight()) ||y <0) {
            yVelocity = yVelocity*-1;
        }
    }
    c.drawBitmap(ball.getBitmap(), x, y, null);  
   //Log.e("sarat",""+touching);

  if(click>=2){
  if(bm_h+y>630){

       final EditText input=new EditText(mContext);
      Toast.makeText(getContext(),"game over",Toast.LENGTH_SHORT).show(); 
      AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
      builder.setTitle("Game Is Over");
        builder.setMessage("Enter your Name");
         // Use an EditText view to get user input.
         input.setId(TEXT_ID);
         builder.setView(input);
         builder.setPositiveButton("save", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub

                name=input.getText().toString();
                Log.d("name",""+name);
                int score=MainActivity.score;
                dbCeation=new DBCeation(mContext);
                dbCeation.insertValues(name, score);
                Intent intent=new Intent(mContext,Home.class);
                click=0;
                mContext.startActivity(intent);
            }
        });
      builder.setIcon(R.drawable.ic_launcher);


      builder.show();
      x=150;
      y=200;
      xVelocity=0;
      yVelocity=0;

  }}
    if(touching){
         // Log.e("game","iftouch called called");
        h.postDelayed(r, FRAME_RATE);    
        bm_w=ball.getBitmap().getWidth();
        bm_h=ball.getBitmap().getHeight();
      }
  }
public void pauseClicked()
{   
    xVelocity=0;
    yVelocity=0;
    invalidate(xVelocity, yVelocity, 0, 0);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
    // Log.d("game","ontouch called");
    int touchType = event.getAction();

     switch(touchType){
        case MotionEvent.ACTION_MOVE: 
            a = event.getX();
            b = event.getY();
            touching = true;

           /* if (dm_touched) {
                x = (int) a - bm_offsetx;
                y = (int) b - bm_offsety;
            }*/
            //invalidate();
            break;

         case MotionEvent.ACTION_DOWN: 
             //x and y give you your touch coordinates
              a = event.getX();
              b = event.getY();
              touching = true;
              if(a>x+20&&a<330&&b<=y+320&&b>y){
                     click++;
                     Log.d("click",""+click);
                 invalidate();
                 touching=true;}
             /* if (touching) {
                    // paused = true;
                    h.removeCallbacks(r);
                    touching = false;
                } else {
                    touching = true;
                }*/
              //Log.d("game","action_down called");
              Log.e("s",""+ a);
              Log.e("s",""+ b);
              Log.e("s",""+ x);
              Log.e("s",""+ y);
              Log.e("s",""+ bm_w);
              Log.e("s",""+ bm_h);
              if ((a > x) && (a < bm_w + x) && (b > y) && (b < bm_h + y)) { 
                  count++;
                MainActivity.setCount(count);
                  //invalidate();
                  Log.i("score",""+count);

                }
              if (dm_touched) {
                  if ((a > x) && (a < bm_w + x) && (b > y) && (b < bm_h + y)) { 
                  move+=2;
                    //x = (int) a - bm_offsetx;
                  y = (int) (yVelocity*-1);

                  //invalidate();

                }}
               // dm_touched = true;
         case MotionEvent.ACTION_UP:
             a = event.getX();
              b = event.getY();
            /* if(a>x+20&&a<330&&b<=y+320&&b>y){
                 click++;
                 Log.d("click",""+click);
             invalidate();
             touching=true;}*/
             if ((a > x) && (a < bm_w + x) && (b > y) && (b < bm_h + y)) { 
                Log.e("game","clicked");
            }

            default:

                dm_touched = true;
            }

            return true;
}

@Override
public void destroyDrawingCache() {

    count=0;
    click=0;
    super.destroyDrawingCache();
}

最佳答案

你可以像下面这样使用它的 cancallable 方法,

builder.setCancelable(false);

关于android - 如何在出现警报对话框后禁用背景屏幕或仅启用警报对话框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21720315/

相关文章:

java - 将不同类型的 SparseArray 传递给函数

java - Android 中的警报框不起作用

java - 如何在多个 TextView 中输入?

android - 扩展的 SQLiteOpenHelper 类每次都调用 onCreate 吗?

android - 在警报对话中自定义 Android 对讲?

java - Android获取文本EditText到AlertDialog

java - 如何在 Android 应用程序的警告对话框中为用户输入编码?

java - 如何在ListView和AlertDialog中设置自定义字体?

java - 从 IP 网络摄像头获取流

java - Android - Scrollview 中的 Gridview 问题