multiple intent's starting by onTouch event

时光毁灭记忆、已成空白 提交于 2020-01-16 11:59:29

问题


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;

} }


回答1:


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.



来源:https://stackoverflow.com/questions/17509749/multiple-intents-starting-by-ontouch-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!