I have a Checkbox that will be the button Follow/Following of my app. How can I change the text of this button in the XML file without have to create a image for that? Is this possible?

Here is my code:

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


    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/follow_card" android:state_checked="false">

        </item>
        <item android:drawable="@drawable/following_card" android:state_checked="true"/>
        <item android:drawable="@drawable/follow_card"/>
    </selector>

Follow_card/Following_card are two diferent XML drawable files defining the shape of the button

有帮助吗?

解决方案

I'd do it programmatically. As the other comment says, you'll need to create an onClickListener, this way:

CheckBox your_checkbox = (CheckBox) findViewById(R.id.your_checkbox_id);
your_checkbox.setOnClickListener(new OnClickListener() { 
  @Override
  public void onClick(View view) {
    if (view.isChecked())
      your_checkbox.setText("Hey I'm checked!");
    else
      your_checkbox.setText("I'm not checked!");
  }
});  
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top