【问题标题】:How do I add a clickable button to a textview that is clickable currently如何将可点击按钮添加到当前可点击的文本视图
【发布时间】:2013-05-16 20:56:40
【问题描述】:

android 新手试图弄清楚一些事情,任何帮助表示赞赏。我有一个当前可点击的文本视图。我只需要为其添加一个可点击的按钮背景。这是我的 xml 文件: 布局下的mainscreen.xml

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:divider="@android:drawable/divider_horizontal"
            android:orientation="vertical"
            android:showDividers="middle" >


             <TextView
                android:id="@+id/mainscreen_option"
                style="@style/TextView.MainscreenItem"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_marginBottom="1dp"
                android:clickable="true"
                android:onClick="onMainscreenClicked"
                android:text="@string/nav_option"
                    />  
</LinearLayout>

我定义按钮的选择器类代码位于可绘制文件夹下: bg_button.xml

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


<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="@drawable/menu_btn_active"/>
<item android:state_pressed="true" android:drawable="@drawable/menu_btn_active" />
<item android:drawable="@drawable/menu_btn" />  
</selector>

文本视图对应的javacode是: mainscreennav.java

private void highlightMenuItem(){
TextView highlightedTextView = null;
final String activeFragmentTitle = getArguments().getString(ACTIVE_MENU_ITEM);
final Resources resources = Application.getAppResources();

if (resources.getString(R.string.nav_option_mainscreen).equals(FragmentTitle)) {
            highlightedTextView = (TextView) getView().findViewById(R.id.nav_option_mainscreen);
        } highlightedTextView.setTextColor(getResources().getColor(R.color.dark_orange));

    }
}

任何人都可以指导我如何将此文本视图转换为按钮,以便两者都可以一起点击,并且我可以将按钮中的文本边距从左侧设置为某些 dp。 提前致谢!贾斯汀

【问题讨论】:

  • 你不能将某样东西“转换”成另一样东西,并且在你完成后拥有两样东西。要么你有一个TextView,要么你有一个Button(在将TextView 转换为Button 之后)。因此,没有可以“可点击”的“两者”。

标签: android android-layout android-intent android-emulator android-listview


【解决方案1】:

编辑:

使用您的代码创建一个选择器 xml 并放入您的可绘制文件夹。我将创建一个 btn_custom.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
    android:drawable="@drawable/btn_active" />
<item android:state_focused="true" android:state_enabled="true"
    android:drawable="@drawable/btn_active" />
<item
     android:drawable="@drawable/btn_default" />
</selector>

然后,在您的 TextView 上:

<TextView android:text="MyButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:layout_marginLeft="20dp"
    android:clickable="true"
    android:background="@drawable/btn_custom"
    android:padding="10dp"/>

在填充时,您可以调整按钮的填充。

在背景上,输入选择器的名称(在我的例子中,@drawable/btn_custom)

你的效果已经开始了。

然后,只需注册一个 onClickListener

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView myTextButton = (TextView) findViewById(R.id.my_button_id);
    myTextButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, "ButtonClick", 200).show();
        }
    });
}

Ps:您可以改用 onClick 方法:P

永远记住:Button 只是一个“样式化”的 TextView。

这是 Button 类的 Android 源代码:

public class Button extends TextView {
    public Button(Context context) {
        this(context, null);
    }

    public Button(Context context, AttributeSet attrs) {
        this(context, attrs, com.android.internal.R.attr.buttonStyle);
    }

    public Button(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
}

是的,就是这样。

【讨论】:

  • 你能解释一下如何以编程方式使文本视图显示为按钮
  • 非常感谢!我会尝试一下,让你知道它是否有效:)
【解决方案2】:

据我了解你的问题,你想在按钮前面有可点击的文本视图吗? 如果是这样创建RelativeLayout 而不是您的LinearLayout,请将您的textview 放在按钮前面(或根据您的需要以其他方式)。并为它们分配 onClick。

希望这对您有所帮助并享受您的工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-16
    • 1970-01-01
    • 2022-11-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多