【问题标题】:How can place a imageButton inside a textfield?如何将 imageButton 放置在文本字段中?
【发布时间】:2017-11-05 14:50:21
【问题描述】:

如何在左侧创建一个带有按钮的文本字段,例如 whatsapp 的笑脸按钮,并且我还希望我的文本在按钮之后开始。

【问题讨论】:

标签: android button textfield


【解决方案1】:

通过 EditText 的 drawableLeft 设置图标,并在下面的代码中执行单击该图标。

 editText.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getRawX() <= (back.getCompoundDrawables()[0].getBounds().width())) {
                    // Your Code
                    return true;
                }
                return false;
            }
        });

【讨论】:

    【解决方案2】:

    这是工作代码:

    <?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="match_parent"
        android:fitsSystemWindows="true">
    
    
        <!-- CONTENT -->
    
        <!-- BOTTOM INPUT SECTION -->
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:background="@android:color/darker_gray"
            android:padding="8dp">
    
            <ImageButton
                android:id="@+id/button_group"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:src="@drawable/ic_group"
                android:background="@android:color/transparent"/>
    
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_toLeftOf="@id/button_group"
                android:layout_marginRight="8dp"
                android:background="@android:color/white"
                android:padding="8dp">
    
                <ImageButton
                    android:id="@+id/button_emoji"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_centerVertical="true"
                    android:src="@drawable/ic_smile_face"
                    android:background="@android:color/transparent"/>
    
                <ImageButton
                    android:id="@+id/button_camera"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:src="@drawable/ic_camera"
                    android:background="@android:color/transparent"/>
    
                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_toRightOf="@id/button_emoji"
                    android:layout_toLeftOf="@id/button_camera"
                    android:layout_marginLeft="8dp"
                    android:layout_marginRight="8dp"
                    android:layout_centerVertical="true"
                    android:hint="Type a message"
                    android:background="@null" />
    
            </RelativeLayout>
        </RelativeLayout>
    </RelativeLayout>
    

    输出:

    【讨论】:

    • 非常感谢
    【解决方案3】:

    将它们一个接一个地放置在 LinearLayout 中,您就可以开始了。你不需要它在里面。结构如下:

    <LinearLayout>
        <Button/>
        <EditText/>
    </LinearLayout>
    

    【讨论】:

    • 我的意思是表格不是文本,只是添加了一张您可以查看的图片
    • 好吗?但我不会在它旁边的文本字段内
    • 当然。将它们放在一起有什么意义?
    • 我不希望它太小,因为按钮也会在右侧。如果我在侧面放两个按钮,它们都必须很小,而且表格也必须缩小
    猜你喜欢
    • 2012-10-19
    • 2018-06-06
    • 1970-01-01
    • 1970-01-01
    • 2021-09-01
    • 2014-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多