【问题标题】:Adding edit text dynamically on click单击时动态添加编辑文本
【发布时间】:2019-07-28 00:15:15
【问题描述】:

我正在开发一个允许用户向图像添加文本的 android 应用程序。我需要在点击时动态地将 EditText 添加到布局中。编辑文本应位于用户在屏幕上单击的任何位置。我已经能够在图像视图上覆盖 EditText,如下所示。如何根据我在屏幕上的点击位置定位它。

布局的XML代码如下:

<FrameLayout
android:id="@+id/frameLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center" >

<EditText
    android:id="@+id/addText"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
</EditText>

<com.infy.CirclesDrawingView
    android:id="@+id/aview"
    android:layout_width="match_parent"
    android:layout_height="300dip" />

</FrameLayout>

也欢迎任何替代品

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    在 onClickListener() 中使用EditText input = new EditText(this);

    【讨论】:

    • 嗨,这有什么帮助。他应该从布局文件中删除它吗
    【解决方案2】:

    借助触摸手势,您将获得 x 和 y 坐标,您可以将编辑文本放置在该坐标上。

    你可以参考这个链接:

    How to get the Touch position in android?

    然后参考这个链接:

    Draw custom View on a specific x\y coordinates

    【讨论】:

      【解决方案3】:

      您需要使用 OnTouchListner 来获取图像被点击的坐标 动态 EditText 并将其放在那里

      imageView.setOnTouchListener(new View.OnTouchListener() {
          @Override
          public boolean onTouch(View v, MotionEvent event) {
              if (event.getAction() == MotionEvent.ACTION_DOWN){
      
                  System.out.println("Touch coordinates : " +
                          String.valueOf(event.getX()) + "x" + String.valueOf(event.getY()));
      //get the dynamic edit text and add it here
              }
              return true;
          }
      });
      

      【讨论】:

        猜你喜欢
        • 2011-12-17
        • 1970-01-01
        • 2012-11-09
        • 1970-01-01
        • 2018-05-10
        • 2023-03-22
        • 2013-07-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多