android中EditText内的可点击按钮(或任何视图)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10829124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:06:55  来源:igfitidea点击:

Clickable Button (or any View) inside EditText in android

androidbuttonandroid-edittext

提问by Rajkiran

I want to have a Button or a clickable View in my EditText so that I can perform some action on click of it. I was able to put a drawable inside my EditText thanks to Marcosbeirigofor this answer.But, now I want to make it clickable so that I can perform some action on it. I know this is possible as HTC uses buttons inside EditText in their stock sms app as shown in the following image-

我想在我的 EditText 中有一个按钮或一个可点击的视图,以便我可以在点击它时执行一些操作。我能够把我的绘制感谢的EditText里面Marcosbeirigo对于这个答案。但是,现在我想让它可点击,以便我可以对它执行一些操作。我知道这是可能的,因为 HTC 在他们的股票短信应用程序中使用 EditText 内的按钮,如下图所示 -

Send button in EditText

EditText 中的发送按钮

In my case, the button will be positioned anywhere, can be in the center also. But the main thing is how can I put a button in EditText?

就我而言,按钮将位于任何位置,也可以位于中心。但主要的是如何在 EditText 中放置一个按钮?

回答by papaiatis

Use RelativeLayout. The Send and Attach buttons are simple Android Buttons and the 32/160 is a TextView. Put the buttons and the textview on the EditText object, play with the layout arrangments and set some right padding to the EditText object so that the text inside it won't go under the buttons.

使用相对布局。发送和附加按钮是简单的 Android 按钮,32/160 是一个 TextView。将按钮和 textview 放在 EditText 对象上,调整布局并为 EditText 对象设置一些正确的填充,以便其中的文本不会在按钮下方。

You don't need any drawable and listening to the click event of the android buttons is not a question anymore. Its absolutely correct

你不需要任何 drawable 并且听 android 按钮的点击事件不再是一个问题。它绝对正确

回答by Gauraw

Use this , It worked for me -

使用这个,它对我有用-

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

            <EditText android:id="@+id/id_search_EditText"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:paddingRight="40dp"
                android:hint="Enter your feelings and connect" />

        <ImageButton android:id="@+id/id_search_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/id_search_EditText"
            android:layout_alignBottom="@+id/id_search_EditText"
            android:layout_alignRight="@+id/id_search_EditText"
            android:background="@drawable/ic_launcher"/>

      </RelativeLayout>

回答by Nam Trung

I think you try to search set click event for compound drawable. You can find some solutions here

我认为您尝试为复合可绘制对象搜索设置单击事件。你可以在这里找到一些解决方案

handling-click-events-on-a-drawable-within-an-edittext

在编辑文本中处理可绘制的单击事件

回答by StErMi

The only possible solution is to use a RelativeLayoutthat allow you to put also Views in overlay to others. Other Layout wont allow you to do such things.

唯一可能的解决方案是使用RelativeLayout,它允许您将视图也放在其他视图中。其他布局不会让你做这样的事情。

Also I found this tutorial: http://developer.android.com/resources/articles/layout-tricks-merge.htmlmaybe it can helps you :)

我也发现了这个教程:http: //developer.android.com/resources/articles/layout-tricks-merge.html也许它可以帮助你:)

回答by Buda Gavril

there is no button inside editText It's just an editText with white background so you don't see it's margins and a simple layout arrangement.

editText 里面没有按钮它只是一个白色背景的editText,所以你看不到它的边距和简单的布局安排。

回答by Ganesh Giri

output

输出

Use this this code may work.

使用此代码可能会起作用。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/REFReLayTellFriend"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="horizontal"
        android:paddingBottom="5dp"
        android:paddingTop="5dp">

        <EditText
            android:id="@+id/txtSearch"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/editext_rounded"
            android:drawableLeft="@android:drawable/ic_menu_search"
            android:gravity="start"
            android:hint="Search"
            android:singleLine="true"/>

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignRight="@+id/txtSearch"
            android:background="@drawable/ic_action_content_filter_list"/>

    </RelativeLayout>

</LinearLayout>