在屏幕上的每次触摸上创建按钮

时间:2012-08-30 05:28:34

标签: android button touch

我想创建一个应用程序,每当我触摸屏幕时,应该在开始时为空白,将创建一个按钮并弹出到屏幕上。所以我在这里尝试的是在main.xml中创建一个按钮并通过id调用它。然后我应用了这个方法。

public class TouchButtonActivity extends Activity {
Button b;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    b=(Button) findViewById(R.id.b1);
    b.setOnTouchListener(nextListener);
}


public OnTouchListener nextListener = new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            float x = event.getX()*event.getXPrecision();
            float y = event.getY()*event.getYPrecision();
            FrameLayout.LayoutParams p = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
            p.gravity = Gravity.TOP;
            p.leftMargin = (int) x;
            p.topMargin = (int) y;
            b.setLayoutParams(p);
            return true;
        }
        return false;
    }
};

}

这是我的xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
 >

<Button
    android:id="@+id/b1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="B" />

</FrameLayout>

我怎么能这样做?

2 个答案:

答案 0 :(得分:4)

如果您想在屏幕中单击任何位置时添加按钮,我认为您可以使用相对布局并在该布局上添加触摸侦听器。触摸该屏幕时,在该布局上添加按钮,以便在新触摸时弹出一个新按钮。谢谢

代码就像 -

  

主要布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rl"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

课程就像 -

rl = (RelativeLayout) findViewById(R.id.rl);
    rl.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            float x = event.getX()*event.getXPrecision();
            float y = event.getY()*event.getYPrecision();

            Button btn = new Button(getApplicationContext());
            RelativeLayout.LayoutParams bp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            bp.leftMargin = (int) x;
            bp.topMargin = (int) y;
            btn.setLayoutParams(bp);
            rl.addView(btn);

            return false;
        }
    });

答案 1 :(得分:0)

我不确定你想要达到的目的,但是:

1)确保您的main.xml布局为FrameLayout

2)然后更改您的代码:

public OnTouchListener nextListener = new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    float x = event.getX()*event.getXPrecision();
                    float y = event.getY()*event.getYPrecision();
                    FrameLayout.LayoutParams p = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                    p.gravity = Gravity.TOP;
                    p.leftMargin = (int) x;
                    p.topMargin = (int) y;
                    b.setLayoutParams(p);
                    return true;
                }
                return false;
            }
        };

修改

你忘了这个:

setOnTouchListener(nextListener);