2015-10-19 114 views
6

我正在编程创建一个开关按钮。我遇到的问题是该按钮不显示ON/OFF文本。这是创建代码:开关按钮不显示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" 

可能会发生什么?

感谢 海梅

+0

后一些更多的代码,所以我们可以知道什么'sw'是 –

+0

SW是开关对象 –

+0

您可以通过RadioGroup和Radio按钮实现此目的。看看我给出的问题的答案stackoverflow.com/questions/23358822/how-to-custom-switch-button/33231991#33231991 –

回答

12

您可以通过XML

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

或者PROGR做到这一点主题为

mSwitch.setShowText(true); 
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; 
} 

}