Android中的开关按钮没有显示ON和OFF文本

16

我正在通过编程创建一个开关按钮,但是问题在于按钮上不显示“ON/OFF”文本。这是创建代码:

        final RelativeLayout.LayoutParams ll = new RelativeLayout.LayoutParams(
              RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        ll.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        sw.setLayoutParams(ll);
        sw.setTextOn(values[0].getName());
        sw.setTextOff(values[1].getName());
        values[0].getName() returns "OK", and values[1].getName() returns "NOK"

可能发生了什么?

谢谢 Jaime


发布一些更多的代码,这样我们就可以知道sw是什么。 - Rahul Tiwari
sw是开关对象。 - Kavin Varnan
你可以通过使用RadioGroup和RadioButton来实现这个功能。请查看我在stackoverflow.com/questions/23358822/how-to-custom-switch-button/33231991#33231991中给出的答案。 - Sanjeet A
5个回答

38

你可以通过 XML 来完成这个操作

<Switch
...
android:showText="true" />

或者编程方式:

mSwitch.setShowText(true);

2

0

原因:

对于我来说,已经设置了 app:showText="true",但是仍然没有显示开/关文本。问题是因为我在其中一个 Switch 父元素上使用了黑暗主题:

<AppBarLayout>
    ....
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    
     <Toolbar>
     
     <ConstraintLayout>
     
        <SwitchCompat>

我花了一些时间才弄清楚原因。

解决方案:

将开关主题更改为浅色主题:

<SwitchCompat>
     ...
     android:theme="@style/Theme.AppCompat.Light"

或者您可以从父级中移除暗色主题,或将其设置为浅色主题。因为如果您的应用程序支持深色模式,文本只会出现在浅色模式中。


0

对我有效的方法是使用

app:showText="true"

而不是

android:showText="true"

-1

希望这能帮到你

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity"
        android:padding="5dp">

        <Switch
            android:id="@+id/mySwitch"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginTop="20dp"
            android:text="Play with the Switch" />

        <TextView
            android:id="@+id/switchStatus"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/mySwitch"
            android:layout_marginTop="22dp"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium" />

    </RelativeLayout>

MainActivity.java

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Switch;
import android.widget.TextView;

public class MainActivity extends Activity {

 private TextView switchStatus;
 private Switch mySwitch;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  switchStatus = (TextView) findViewById(R.id.switchStatus);
  mySwitch = (Switch) findViewById(R.id.mySwitch);

  //set the switch to ON 
  mySwitch.setChecked(true);
  //attach a listener to check for changes in state
  mySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {

   @Override
   public void onCheckedChanged(CompoundButton buttonView,
     boolean isChecked) {

    if(isChecked){
     switchStatus.setText("Switch is currently ON");
    }else{
     switchStatus.setText("Switch is currently OFF");
    }

   }
  });

  //check the current state before we display the screen
  if(mySwitch.isChecked()){
   switchStatus.setText("Switch is currently ON");
  }
  else {
   switchStatus.setText("Switch is currently OFF");
  }
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.activity_main, menu);
  return true;
 }

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接