【问题标题】:Buttons on EditText in Android [duplicate]Android中EditText上的按钮[重复]
【发布时间】:2012-12-15 00:21:57
【问题描述】:

可能重复:
Android Labels or Bubbles in EditText

我怎样才能像这样在EditText 上放置按钮:

我只知道如何在EditText 的末尾添加“X”,但我无法根据需要说明如何使用它。当然我说的是安卓。

【问题讨论】:

标签: android button android-edittext


【解决方案1】:

如果你想要一个按钮在你的编辑文本的右边,你可以像这样将它包裹在一个水平线性布局中

<LinearLayout android:orientation="horizontal"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content">

<EditText 
    android:id="@+id/editSearchContact"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"/>

<Button android:id="@+id/xbutton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
</LinearLayout>

如果你想要一个实际的 X 使用图像按钮。

编辑:哦,为了获得更多控制,您可以使用android:weight="" 属性为编辑文本指定屏幕宽度的比例以展开。

【讨论】:

  • 谢谢,但我知道如何使用 X 按钮。我想要这个带有名字的按钮
  • @user1189159 哦,“x”是未知的,而不是“x”,如取消。我的错
【解决方案2】:

另一个选项(如果我理解你想要什么)是这样的:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <!-- This LinearLayout will be your EditText that holds two Buttons with name and also image-->    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:drawable/editbox_background" >

        <Button
            android:id="@+id/the_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginRight="7dp"
            android:background="@drawable/draw_the_button"
            android:padding="5dp"
            android:text="Name Here" />

        <EditText
            android:id="@+id/editText1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@null"
            android:layout_gravity="center_vertical" />

        <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@null"
            android:src="@android:drawable/ic_input_add"
            android:layout_gravity="center_vertical"  />
    </LinearLayout>

</LinearLayout>

试试看。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-10
    • 2018-01-30
    • 1970-01-01
    • 2018-05-24
    • 1970-01-01
    • 2018-06-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多