【问题标题】:Make EditText clickable使 EditText 可点击
【发布时间】:2015-09-09 16:11:23
【问题描述】:

晚上好, 我有以下布局,我想让编辑文本随处可点击。此外,如果 layout_height 设置为 match_parent,则编辑文本可点击区域的工作方式类似于 wrap_content。

这是布局。

<?xml version="1.0" encoding="utf-8"?>

    <RelativeLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"  
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:focusable="true"
        android:focusableInTouchMode="true">

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar"/>

     <RelativeLayout
         android:id="@+id/relative"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_below="@+id/toolbar">   

    <TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="20dp"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:paddingRight="20dp">        

    <EditText 
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    </TextInputLayout>
    </RelativeLayout>

    <ScrollView    
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/relative">

     <RelativeLayout
         android:layout_width="wrap_content"
         android:layout_height="wrap_content">  

    <TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginEnd="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp">     

    <EditText
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:hint="@string/note_content"
        android:background="@null"      
        android:gravity="top"/>     

    </TextInputLayout>
    </RelativeLayout> 
    </ScrollView>

    </RelativeLayout>

如何使编辑文本在滚动视图内的相对布局中随处可点击?

【问题讨论】:

  • 澄清一下:您的意思是要单击布局中的任意位置以向 EditText 注册? (例如,如果用户单击 EditText 上方或下方,仍应选择 EditText)
  • 其实编辑文本只有在文本所在的区域才可以点击。我希望它是完全可复制的。
  • EditText 在没有文字的时候不能点击吗?我错过了什么吗?
  • 可点击但可点击区域为wrap_content

标签: java android xml


【解决方案1】:

虽然我不明白您为什么要这样做,但您可以制作一个将 invisible 属性设置为 true 的按钮,以覆盖整个布局。然后在用户触摸这个隐形按钮时做出响应。

您使用名为 setOnEditorActionListener() 的 EditText 方法进行响应

查看此页面上标题为“响应操作按钮事件”的信息: http://developer.android.com/guide/topics/ui/controls/text.html

【讨论】:

    【解决方案2】:

    scrollView 中,您无法填充整个视图,直到此处有内容,您要求使EditText 填充除TextView 之外的整个父空间,以便您可以单击任何部分布局的重点是EditText。除非你给 EditText 一个 layout_height,否则它将保持换行,因为布局在 RelativeLayout' which is a child for of theScrollView` 中。

    【讨论】:

      猜你喜欢
      • 2017-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-14
      • 1970-01-01
      相关资源
      最近更新 更多