首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ToggleButton不工作

ToggleButton不工作
EN

Stack Overflow用户
提问于 2014-03-13 06:36:48
回答 4查看 1.2K关注 0票数 0

我是机器人新手。我尝试创建一个简单的切换按钮并捕捉它的状态变化。但当我在我的设备上运行它时它就崩溃了。

以下是MainActivity.java代码:

代码语言:javascript
运行
复制
package com.example.addimagebutton;

import android.os.Bundle;
import android.app.Activity;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity extends Activity {

    private ToggleButton tbSwitch;

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

        tbSwitch = (ToggleButton) findViewById(R.id.swButton);
        tbSwitch.setOnCheckedChangeListener(toggleListener);
    }

    OnCheckedChangeListener toggleListener = new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            // TODO Auto-generated method stub
            if (isChecked) {
                Toast.makeText(getApplicationContext(), "On",
                        Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(getApplicationContext(), "Off",
                        Toast.LENGTH_SHORT).show();
            }
        }
    };
}

以下是activity_main.xml代码:

代码语言:javascript
运行
复制
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Switch
        android:id="@+id/swButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Switch"
        android:textOff="Off"
        android:textOn="On" />

</LinearLayout>

我试图在没有监听器对象的情况下运行这段代码。(注释这些),但是它给了我同样的结果。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2014-03-13 06:40:33

您正在切换到按钮,因此它给您ClassCastException..。

改变这个

代码语言:javascript
运行
复制
 <Switch
    android:id="@+id/swButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Switch"
    android:textOff="Off"
    android:textOn="On" />

转到

代码语言:javascript
运行
复制
<ToggleButton
    android:id="@+id/swButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Switch"
    android:textOff="Off"
    android:textOn="On" />
票数 2
EN

Stack Overflow用户

发布于 2014-03-13 06:40:51

使用ToggleButton而不是Switch

代码语言:javascript
运行
复制
   <ToggleButton
    android:id="@+id/swButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Switch"
    android:textOff="Off"
    android:textOn="On"" />

因为您的布局包含Switch并使用ToggleButton进行转换。那是不对的

票数 1
EN

Stack Overflow用户

发布于 2014-03-13 06:41:17

switch更改为ToggleButton

代码语言:javascript
运行
复制
   <ToggleButton 
        android:id="@+id/swButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Switch"
        android:textOff="Off"
        android:textOn="On" />
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22370899

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档