Android 中的开关按钮不显示打开和关闭文本

Switch button does not show ON and OFF text in Android

我正在以编程方式创建一个切换按钮。我遇到的问题是按钮不显示 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"

可能发生了什么?

谢谢 海梅

希望这会有所帮助

<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;
 }

}

您可以通过 XML

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

或编程为

mSwitch.setShowText(true);

这是Android-API的回答。你必须标记它可以显示标签的开关!!

public void setShowText (boolean showText) Added in API level 21

Sets whether the on/off text should be displayed.

Related XML Attributes:

android:showText

Parameters showText true to display on/off text

http://developer.android.com/reference/android/widget/Switch.html#setShowText%28boolean%29

对我有用的是使用

app:showText="true"

而不是

android:showText="true"

原因:

对我来说,设置了app:showText="true",但ON/OFF文本仍然没有显示;问题是因为我在 Switch parent 之一上使用了深色主题,例如:

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

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

解法:

将切换主题更改为浅色主题:

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

或者您可以从 parent 中删除深色主题或将其设置为浅色主题。因为如果你的应用支持深色模式,文本只会出现在浅色模式下。