android - 将按钮样式设置为看起来像 EditText(但仍然表现得像按钮)

标签 android android-edittext android-button

我意识到这样做有点奇怪,但我有一个按钮需要看起来像一个 EditText,但仍然像一个按钮。我的布局 XML 目前看起来像这样:

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    style="?android:attr/editTextStyle" />

这使它具有 EditText 的外观,但也通过阻止触发 onClick 事件,除非按钮具有焦点(有效地使其需要两次单击),从而稍微扰乱了行为。有没有办法在不改变按钮行为的情况下保持样式?

我想只制作一个看起来像 EditText 的九色补丁背景,但有这么多不同的 Android 版本和皮肤,如果可能的话我宁愿使用系统样式。

最佳答案

行为类似于按钮的 EditText 怎么样?

<EditText android:id="@+id/Edit"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:clickable="true"/>

您也可以为它定义一个 OnClickListener。而且它不会获得焦点。

关于android - 将按钮样式设置为看起来像 EditText(但仍然表现得像按钮),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8303269/

相关文章:

android - 如何通过android中的代码从edittext中删除可绘制图像

Android 5.0 - ProgressBar 不能显示在按钮上

android - 如何禁用Android按钮?

android - 使用按钮在选项卡之间切换

java - 致命异常 : android. app.RemoteServiceException Context.startForegroundService() 然后没有调用 Service.startForeground()

android - 在 ListView 项目中启动 AnimationDrawable。元素什么时候附加?

android - 多个(动态)EditText 和 onFocusChange 问题

android - 允许用户访问 wifi 设置而不给予自由支配权

java - 如果我按 Enter 或关闭键盘,EditText 会更改大小

android - 如何在单击 android 中的按钮时重定向到特定的 URL?