【问题标题】:Checkbox in Android -> running errorAndroid中的复选框->运行错误
【发布时间】:2014-04-14 03:32:53
【问题描述】:

我正在尝试制作一个带有 3 个复选框和 1 个按钮的简单应用程序。我想为每个复选框制作一个 toast 以查看它是否被选中(单击侦听器),然后为按钮实现一个单击侦听器以发布一般消息以查看是否选中了女巫复选框。

如果我只使用它在我的手机上运行的简单按钮(Android 版本 4.1.1)放置代码,但如果我实现这 3 个复选框,它会崩溃(我收到类似“你的应用程序停止运行,单击强制关闭)。

我的 xml:

<?xml version="1.0" encoding="UTF-8"?> 
<LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
     android:gravity="center"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
>
    <Checkbox
         android:id= "@+id/ok_checkbox"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:text="@string/ok_read"
    />
    <Checkbox
         android:id= "@+id/removed_checkbox"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:text="@string/misspelled"
    />
    <Checkbox
         android:id= "@+id/changed_checkbox"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:text="@string/changed_read"
    />    
   <Button
         android:id= "@+id/final_click"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="@string/final_click" 
    />

  </LinearLayout>

活动:

package com.flowerPower.SpellingTest;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;

public class SecondActivity extends Activity {

    private Button finalB;
    private CheckBox ok, changed, removed;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.second);

        finalB = (Button) findViewById(R.id.final_click);
        ok = (CheckBox) findViewById(R.id.ok_checkbox);
        changed = (CheckBox) findViewById(R.id.changed_checkbox);
        removed = (CheckBox) findViewById(R.id.removed_checkbox);

        ok.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                if (((CheckBox) v).isChecked()) {
                    Toast.makeText(SecondActivity.this, "Your choice is OK",
                            Toast.LENGTH_LONG).show();
                }
            }
        });
        changed.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (((CheckBox) v).isChecked()) {
                    Toast.makeText(SecondActivity.this, "Your choice is changed",
                            Toast.LENGTH_LONG).show();
                }
            }
        });
        removed.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (((CheckBox) v).isChecked()) {
                    Toast.makeText(SecondActivity.this, "Your choice is Windows",
                            Toast.LENGTH_LONG).show();
                }
            }

        });

    }

 }

我该怎么办?

【问题讨论】:

    标签: android xml checkbox


    【解决方案1】:

    对于复选框,像这样使用 setOnCheckedChangeListener()-

            ok.setOnCheckedChangeListener(new OnCheckedChangeListener() 
            {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
                {
                    if(isChecked)
                    {
                        Toast.makeText(SecondActivity.this, "Your choice is OK",
                                Toast.LENGTH_LONG).show();
                    }
                }
            });
    

    对于其他复选框也是如此。

    【讨论】:

      猜你喜欢
      • 2012-05-31
      • 1970-01-01
      • 1970-01-01
      • 2019-03-13
      • 2015-02-22
      • 2019-03-07
      • 1970-01-01
      • 1970-01-01
      • 2014-06-17
      相关资源
      最近更新 更多