【问题标题】:Android setTextOn not working in onCheckChanged methodAndroid setTextOn 在 onCheckChanged 方法中不起作用
【发布时间】:2015-04-23 00:52:36
【问题描述】:

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

                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"); 

代码:

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有问题吗?

【问题讨论】:

  • 我不认为您对setTextOnsetTextOff 的调用需要在if 中——它们只是定义了开关在打开或关闭时的显示方式,因此它们不需要有条件地设置。参考:API

标签: java android settext


【解决方案1】:

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"/>

【讨论】:

    【解决方案2】:

    @Simon M 观察到,这个 xml 和 java sn-p 产生一致的输出,如下面的屏幕所示。

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

    编辑

    以下 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!");
            }
        });
    

    ...这是这样的:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多