Android setTextOn无法在onCheckChanged方法中使用

时间:2015-04-23 00:52:37

标签: java android settext

接下来的两个语句会在左下方生成正确的屏幕:

                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 here enter 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 个答案:

答案 0 :(得分:3)

setTextOnsetTextOff函数用于根据Switch的状态设置标签。

文本“开关是:开”只是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 :(得分: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