Android 如何在Animatedview中从AlertDialog调用意图?
我有游戏,在游戏结束后,我使用AlertDialog和EditText和按钮输入名字,最后我必须保存当前的分数和球员名字到数据库中,当我点击按钮时,我可以保存分数和名字。我的要求是,当我点击同一个按钮时,我想切换到游戏主页(HomeActivity)的意图 我的代码是:Android 如何在Animatedview中从AlertDialog调用意图?,android,android-animation,Android,Android Animation,我有游戏,在游戏结束后,我使用AlertDialog和EditText和按钮输入名字,最后我必须保存当前的分数和球员名字到数据库中,当我点击按钮时,我可以保存分数和名字。我的要求是,当我点击同一个按钮时,我想切换到游戏主页(HomeActivity)的意图 我的代码是: public class AnimatedView extends ImageView{ String name; static int count=0; private Context mContext; int x = 13
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();
}
}
public类AnimatedView扩展了ImageView{
字符串名;
静态整数计数=0;
私有上下文;
int x=130;
int y=450;
私人浮动a、b;
私有int xVelocity=20;
私人国际城市=20;
私有处理器h;
专用最终整数帧_率=25;
可牵引球;
布尔接触;
布尔值dm_=false;
浮动移动=3;
int bm_x=0,bm_y=0,bm_offsetx,bm_offsety,bm_w,bm_h;
布尔暂停;
私人绘制线,ball1,背景;
静态int click=0;
私有静态最终整型文本\u ID=0;
DBCeation DBCeation;
公共动画视图(上下文){
超级(上下文);
}
公共动画视图(上下文,属性集属性){
超级(上下文,attrs);
mContext=上下文;
h=新处理程序();
}
private Runnable r=new Runnable(){
@凌驾
公开募捐{
//Log.e(“游戏”、“跑步”);
如果(触摸=真)
使无效();
}
};
@凌驾
受保护的空白onDraw(画布c){
BitmapDrawable ball=(BitmapDrawable)mContext.getResources().getDrawable(R.drawable.ball);
如果(x(this.getHeight()-ball.getBitmap().getHeight())| | y=2){
如果(bm_h+y>630){
最终编辑文本输入=新编辑文本(mContext);
Toast.makeText(getContext(),“游戏结束”,Toast.LENGTH_SHORT.show();
AlertDialog.Builder=新建AlertDialog.Builder(mContext);
builder.setTitle(“游戏结束”);
builder.setMessage(“输入您的姓名”);
//使用EditText视图获取用户输入。
输入.setId(文本\u ID);
builder.setView(输入);
setPositiveButton(“保存”,新的DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
//TODO自动生成的方法存根
name=input.getText().toString();
Log.d(“名称”,“名称+名称”);
int score=MainActivity.score;
dbCeation=新dbCeation(mContext);
dbCeation.insertValues(名称、分数);
//此代码不工作,并给出编译时错误
Intent Intent=新的Intent(getContext(),MainActivity.class);
星触觉(意向);
}
});
builder.setIcon(R.drawable.ic_启动器);
builder.show();
x=150;
y=200;
xVelocity=0;
yVelocity=0;
}}
如果(触摸){
//Log.e(“游戏”,“如果触摸被调用”);
h、 后延迟(r,帧速率);
bm_w=ball.getBitmap().getWidth();
bm_h=ball.getBitmap().getHeight();
}
}
使用mContext
您已经在mContext
Intent intent=new Intent(mContext,MainActivity.class);
mContext.startActivity(intent);
使用
mContext
您已经在mContext
Intent intent=new Intent(mContext,MainActivity.class);
mContext.startActivity(intent);
试试这个代码
Intent intent=new Intent(mContext,MainActivity.class);
startActivity(intent);
试试这个代码
Intent intent=new Intent(mContext,MainActivity.class);
startActivity(intent);
我希望这对你有帮助
Intent intent=new Intent(mContext,MainActivity.class);
mContext.startActivity(intent);
否则就试试看
mContext.getApplicationContext.startActivity(intent);
我希望这对你有帮助
Intent intent=new Intent(mContext,MainActivity.class);
mContext.startActivity(intent);
否则就试试看
mContext.getApplicationContext.startActivity(intent);
当您在代码中从AlertDialog启动活动时,请使用该上下文启动活动
Intent intent=new Intent(mContext,MainActivity.class);
mContext.startActivity(intent);
当您在代码中从AlertDialog启动活动时,请使用该上下文启动活动
Intent intent=new Intent(mContext,MainActivity.class);
mContext.startActivity(intent);
无法写入startActivity(intent);这会导致编译时错误。对于类型new DialogInterface,startActivity(intent)方法未定义。OnClickListener(){}@bharat我已编辑了我的答案,您需要使用McContext调用startActivity。@bharat是否正在工作?无法写入startActivity(intent);它给出编译时错误类型new DialogInterface的方法startActivity(Intent)未定义。OnClickListener(){}@bharat我已经编辑了我的答案,您需要使用McContext调用startActivity。@bharat现在是否工作?使用
McContext.startActivity(Intent)
哪一个将获得启动活动的上下文如果有效接受适合您需要的答案使用mContext.startActivity(intent);
哪一个将获得启动活动的上下文如果有效接受适合您需要的答案finish()
将结束您创建的活动,因此我将删除它finish()
将结束您创建的活动,因此我将删除它