0

我是 Android 的新手,在我的应用程序中,我需要在手指触摸时绘制 textview/editview,用户可以在其中输入。我搜索了很多,但没有发现任何相关内容。

我找到了绘制编辑视图/文本的链接,但不是在手指触摸上。

4

1 回答 1

1

您可以通过在视图上注册触摸事件来完成此操作。当触摸事件触发时,您可以根据触摸事件坐标创建一个 EditText/TextView。

class YourMainClass extends Activity{

  public void onCreate(Bundle bundle)
  {
     //Do your normal UI initialization here
     your_layout.setOnTouchListener(new TouchListener()); //where your_layout is the layout/view of your Activity that should register the touch events.
  }

  class TouchListener implements OnTouchListener
  {
     @Override
     public boolean onTouch(View v, MotionEvent event) {
         if(event.getAction() == MotionEvent.ACTION_UP) {
           RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT); //See note below
           params.leftMargin = (int)event.getX() - v.getLeft();
           params.topMargin = (int)event.getY() - v.getTop();
           EditText edit = new EditText(this);
           edit.setLayoutParams(params);
           your_layout.addView(edit);
           return true;
         }
     }
  }
}

注意:确保将 LayoutParams 类型更改为您使用的布局类型(例如:LinearLayout.LayoutParams,如果您使用 LinearLayout 来放置 EditText)。

v.getLeft()此外,如果您使用填充,实际 EditText 的坐标可能会关闭(因为使用and时填充区域仍被计为视图v.getTop(),但在将 EditText 添加到布局时不计为视图)。

于 2012-09-16T17:02:00.167 回答