سؤال

I want to write a program which creates a button, whenever I'm touching the screen, the button should be created on the place where the screen was touched.

I wrote a program which creates circles, whenever and where the screen is touched, can anybody explain me how to make buttons instead of circles?

Thx.

Main Activity

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View.OnTouchListener;

public class SingleTouchActivity extends Activity  {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(new SingleTouchEventView(this, null));
  }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.single_touch, menu);
        return true;
    }



}

Touch Activity

import java.util.ArrayList; //not all imports are necessary
import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.view.View.OnClickListener;


public class SingleTouchEventView extends RelativeLayout {
  private Paint paint = new Paint();
  List<Point> points = new ArrayList<Point>();



  public SingleTouchEventView(Context context, AttributeSet attrs) {
    super(context, attrs);}



@Override
public boolean onTouchEvent(MotionEvent event) {
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
    case MotionEvent.ACTION_MOVE:  // a pointer was moved
    case MotionEvent.ACTION_UP:

        Point p = new Point();
        p.x = (int)event.getX();
        p.y = (int)event.getY();
        points.add(p);

        Button button = new Button(getContext());

        int buttonHeight = 50;
        int buttonWidth = 50;

        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(buttonWidth, buttonHeight);
        params.leftMargin = p.x;
        params.topMargin = p.y;

        addView(button, params);


        button.setOnClickListener(new OnClickListener() {
            public void onClick(View v)
            {
                //DO SOMETHING! {RUN SOME FUNCTION ... DO CHECKS... ETC}
            } 
        });

    case MotionEvent.ACTION_CANCEL: {
      break;
    }
    }
    invalidate();
    return true;  

}

        // Do something
}
هل كانت مفيدة؟

المحلول

First make your class extends a ViewGroup like RelativeLayout and FrameLayout in your case.

Then, on the touch event, create a Button (or a ImageView and setOnClickListener()), adjust the position of the button with margins on the view LayoutParams and add the button to the layout via addView().

Edit: The button creation will be like this on onTouchEvent():

Point p = new Point();
p.x = (int)event.getX();
p.y = (int)event.getY();
Button button = new Button(context);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(buttonWidth, buttonHeight);
params.leftMargin = p.x;
params.topMargin = p.y;
addView(button, params);

Also change this to be inside ACTION_UP to prevent creating multiple buttons on long pressing.

نصائح أخرى

This is not possible. You can draw a circle because that is a graphic. If you were to make button appear then you would need to add it to XML. I assume you are making a game that requires elements to randomly appear. I suggest if this is the case to use and engine a spawn random circles and when touched do what you need. Hope this helps

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top