2015-04-23 22 views
2

下面兩個語句產生正確屏幕左下:的Android setTextOn不onCheckChanged方法工作

   if(isChecked) sw.setText ("This switch is: On"); 
       else   sw.setText ("This switch is: Off"); 

根據我在看文字,這接下來的兩個語句應該農產品屏上留下的。但是,他們生產不正確屏幕右下:

   if(isChecked) sw.setTextOn ("This switch is: On"); 
       else   sw.setTextOff("This switch is: Off"); 

enter image description hereenter image description here

代碼:

package com.dslomer64.checkbox; 

import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.CheckBox; 
import android.widget.CompoundButton; 
import android.widget.Switch; 


public class MainActivity extends ActionBarActivity{//} implements CompoundButton.OnCheckedChangeListener { 
    CheckBox cb; 
    Switch sw; 

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

     cb = (CheckBox)findViewById(R.id.check); 
     cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       if(isChecked) 
        cb.setText("checked"); 
       else 
        cb.setText("Unchecked"); 
      } 
     }); 

     sw = (Switch)findViewById(R.id.swish); 
     sw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       if(isChecked) 
        sw.setTextOn("This switch is: On"); //////////// 
       else 
        sw.setTextOff("This switch is: Off"); ////////// 

      } 
     }); 
    } 
} 

的XML:

<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" 
       android:paddingLeft="@dimen/activity_horizontal_margin" 
       android:paddingRight="@dimen/activity_horizontal_margin" 
       android:paddingTop="@dimen/activity_vertical_margin" 
       android:paddingBottom="@dimen/activity_vertical_margin" 
       tools:context=".MainActivity" android:weightSum="1"> 

    <Switch 
     android:layout_width="453dp" 
     android:layout_height="100dp" 
     android:id="@+id/swish" 
     android:layout_gravity="center_vertical" 
     android:layout_alignParentTop="true" android:layout_centerHorizontal="true"/> 

    <CheckBox 
     android:text="Unchecked" 
     android:layout_width="200dp" 
     android:layout_height="100dp" 
     android:id="@+id/check" 
     android:layout_centerVertical="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true"/> 

</RelativeLayout> 

是書錯setTextOnsetTextOff?我在java代碼或xml中遇到問題嗎?

+2

我不認爲你對'setTextOn'和'setTextOff'的調用需要在'if'中 - 它們只是定義了在開啓或關閉時如何顯示切換,所以它們不需要有條件地設置。參考:[API](http://developer.android.com/reference/android/widget/ToggleButton.html#setTextOff%28java.lang.CharSequence%29) –

回答

2

setTextOnsetTextOff函數用於根據Switch的狀態設置標籤。

文本「開關是:開」只是您的開關的標籤,並不代表傳達您的Switch的狀態。

爲了達到你想要的結果,你需要調用setShowText(true)

sw = (Switch)findViewById(R.id.swish); 
sw.setShowText(true); 

,或者你可以在你的XML添加。

<Switch 
     android:layout_width="453dp" 
     android:layout_height="100dp" 
     android:id="@+id/swish" 
     android:layout_gravity="center_vertical" 
     android:layout_alignParentTop="true"  
     android:showText="true"/> 
1

正如@Simon M所觀察到的,這個xml和java片段會產生一致的輸出,如下面的屏幕所示。

<Switch 
    android:layout_width="453dp" 
    android:layout_height="100dp" 
    android:id="@+id/swish" 
    android:layout_gravity="center_vertical" 
    android:textOn="ON!!" 
    android:textOff="OFF!" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true"/> 



    sw = (Switch)findViewById(R.id.swish); 
    sw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
      // absolutely nothing 
     }); 

enter image description here

編輯

下面的XML和Java產生什麼樣的案文瞄準:

<Switch 
    android:layout_width="453dp" 
    android:layout_height="100dp" 
    android:id="@+id/swish" 
    android:layout_gravity="center_vertical" 
    android:layout_alignParentTop="true" 
    android:text="This switch is...OFF" 
    android:checked="false" 
    android:layout_centerHorizontal="true"/> 



    sw = (Switch)findViewById(R.id.swish); 
    sw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
      if(isChecked) 
       sw.setText("Switch is ON!"); 
      else 
       sw.setText("Switch is OFF!"); 
     } 
    }); 

...這是這樣的:

enter image description here