每次触摸屏幕时创建按钮

每次触摸屏幕时创建按钮

本文介绍了每次触摸屏幕时创建按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个应用程序,只要我触摸屏幕(开始时应该为空白),就会创建一个按钮并将其弹出到屏幕上.所以我在这里尝试的是在main.xml中创建一个按钮,并通过id对其进行调用.然后我应用了这种方法.

I want to make an application where whenever I touch the screen, which should be blank at the start, a button will be created and pop up onto the screen. So what I tried here is to make a button in main.xml and call it by id. Then I applied this method.

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>

我该怎么做?

推荐答案

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

If you want to add a button while you click any where in the screen i think you can use a Relative layout and add touch listener on that layout. While touching on that screen, add button on that layout so it will be like on new touch a new button will be popped up. Thank you

代码就像-

<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;
        }
    });

这篇关于每次触摸屏幕时创建按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 10:35