Android中的切换按钮不显示ON和OFF文本

时间:2015-10-19 13:06:17

标签: android android-layout android-switch

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

可能会发生什么?

由于 海梅

3 个答案:

答案 0 :(得分:20)

您可以通过XML

执行此操作
<Switch
...
android:showText="true" />

或以编程方式

mSwitch.setShowText(true);

答案 1 :(得分: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

答案 2 :(得分:0)

希望这会有所帮助

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

}