由onTouch事件开始的多个意图 [英] multiple intent's starting by onTouch event

查看:60
本文介绍了由onTouch事件开始的多个意图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们,如果用户单击特定位置,我将开始创建意图.第一次触摸时,他打开菜单,然后第二次打开活动.问题是,开始了许多具有相同意图的副本

Hey guys i am getting to start an intent if a user clicks on specific location.at first touch he opens a menu and on second he opens the activity.The problem is that many copy's of same intent are started

 import android.app.Activity;
 import android.content.Context;
 import android.content.Intent;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
  import android.graphics.Canvas;
 import android.graphics.Color;
 import android.graphics.Paint;
 import android.graphics.Paint.Align;
 import android.os.Bundle;
import android.text.TextPaint;
import android.view.MotionEvent;
 import android.view.View;
 import android.view.View.OnTouchListener;
 import android.widget.Toast;

  public class gfx extends Activity implements OnTouchListener{
Bitmap a,b;
gfx1 drw;
String a1;
boolean flag=false,flag1=false,flag2=false;
Canvas c1;
float x=0,y=0,z=0,bitx=0,bity=0;
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    drw = new gfx1(this);
    drw.setOnTouchListener(this);
    setContentView(drw);
}

public class gfx1 extends View implements Runnable {

    public gfx1(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        a = BitmapFactory.decodeResource(getResources(),
                R.drawable.greenball);

    }
    void callin(String a1)
    {
        Intent inte = new Intent(a1);
        startActivity(inte);
    }

    @Override
    protected void onDraw(Canvas c1) {
        // TODO Auto-generated method stub
        super.onDraw(c1);
        c1.drawColor(Color.YELLOW);
        b=Bitmap.createScaledBitmap(a,c1.getWidth()/3, c1.getHeight()/3, true);
        bitx=(c1.getWidth()/2)-(a.getWidth()/2);
        bity=(c1.getHeight()/2)-(a.getHeight()/2);
        c1.drawBitmap(a,  bitx, bity, null);

        Paint textPaint = new Paint();
        textPaint.setARGB(50, 254, 10, 50);
        textPaint.setTextAlign(Align.CENTER);
        textPaint.setTextSize(30);
        if(flag)
        {
            c1.drawText("clicked",300, 300,textPaint);
            c1.drawBitmap(b,(c1.getWidth()/2)-(b.getWidth()/2),(c1.getHeight()/2)+(a.getHeight()/2), null);
            c1.drawBitmap(b,(c1.getWidth()/2)-(b.getWidth()/2),(c1.getHeight()/2)-(a.getHeight()/2)-(b.getHeight()), null);
        }
        float bitbx1=(c1.getWidth()/2)-(b.getWidth()/2);
        float bitbx2=(c1.getWidth()/2)+(b.getWidth()/2);
        float bitby1=(c1.getHeight()/2)-(b.getHeight())-(a.getHeight()/2);
        float bitby2=((c1.getHeight()/2)-(a.getHeight()/2));
        if(flag2)
        {
            c1.drawText("Opening...please wait", 600, 600, textPaint);
            flag1=true;
            if(flag1)
            {
                a1="com.example.claci.MAINACTIVITY";
            callin(a1);
            }
            flag1=false;
        }

        if((x>bitx&&x<bitx+(a.getWidth()))&& (y>bity&&y<bity+(a.getHeight())))
        {
            flag=true;

        }
        if((x>bitbx1&&x<bitbx2)&&(y>bitby1&&y<bitby2))
        {
    if(flag)
    {
                flag2=true;
    }
        }
        invalidate();

    }

    @Override
    public void run() {
        // TODO Auto-generated method stub

    }

}

@Override
public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub


    switch(event.getAction()){
    case MotionEvent.ACTION_DOWN:
        x=event.getX();
        y=event.getY();
        break;
}
    return true;

}}

推荐答案

更好的结构是创建菜单,以及随后的onTouch方法调用的活动创建.如psink所述,这不是对onDraw的正确使用,它不必要地链接了两个非常不相关的东西.

A much better structure would be to have your menu creation, and subsequent activity creation called from onTouch method. As psink mentioned, that is not the proper use of onDraw, and it unnecessarily links tow very unrelated things.

我还将使用两个类变量-一个是菜单是否存在的标志,另一个是对新创建的活动的引用.如果一项活动已经存在,则不会创建另一项活动.但是,当现有活动完成时,它需要将结果返回给该活动,因此您可以清除该引用,并准备在需要时创建一个新引用.

I would also use two class variables-- one that is a flag for if the menu exists yet, and another that is a reference to the newly created activity. If an activity already exists, you don't create another one. When the existent activity completes, though, it needs to return a result to this activity so you can clear that reference and be ready to create a new one when needed.

这篇关于由onTouch事件开始的多个意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆