gpt4 book ai didi

android - 如何在 Animatedview 中调用 AlertDialog 的 Intent ?

转载 作者:行者123 更新时间:2023-11-29 15:18:23 26 4
gpt4 key购买 nike

我有游戏,在游戏结束后,我使用带有 EditText 和 Button 的 AlertDialog 输入名称,最后我必须在单击按钮时将当前分数和玩家名称保存到数据库中,很好,我可以保存分数和名字。我的要求是当我点击同一个按钮时,我想改变到游戏主页(HomeActivity)的 Intent 。

我的代码是:

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);
//this code is not working and gives compile time error
Intent intent=new Intent(getContext(),MainActivity.class);
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();
}
}

最佳答案

试试这段代码

Intent intent=new Intent(mContext,MainActivity.class);
startActivity(intent);

关于android - 如何在 Animatedview 中调用 AlertDialog 的 Intent ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21719656/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com