15

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

可能会发生什么?

谢谢詹姆

4

5 回答 5

33

您可以通过 XML 做到这一点

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

或以编程方式作为

mSwitch.setShowText(true);
于 2015-10-19T13:14:34.160 回答
2

这是来自 Android-API 的答案。您必须标记它可以显示标签的开关!

public void setShowText (boolean showText) 在 API 级别 21 中添加

设置是否显示开/关文本。

相关 XML 属性:

android:showText

参数 showText true 显示开/关文本

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

于 2015-10-19T13:19:13.737 回答
0

对我有用的是使用

app:showText="true"

而不是

android:showText="true"
于 2021-12-24T19:10:11.237 回答
0

原因:

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

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

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

解决方案:

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

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

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

于 2022-02-22T20:31:00.073 回答
-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;
 }

}
于 2015-10-19T13:09:58.370 回答