爱玺玺

爱玺玺的生活日记本。wx:lb87626

android复选框CheckBox的操作

package com.example.lenovo.app0;


import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.CheckBox;

import android.widget.CompoundButton;

import android.widget.TextView;


public class MainActivity extends AppCompatActivity {

    private CheckBox checkBox1;

    private CheckBox checkBox2;

    private CheckBox checkBox3;

    private TextView textView;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        textView=(TextView)findViewById(R.id.viewText);

        checkBox1=(CheckBox)findViewById(R.id.check1);

        checkBox2=(CheckBox)findViewById(R.id.check2);

        checkBox3=(CheckBox)findViewById(R.id.check3);

        MyCheckChange myCheckChange=new MyCheckChange();

        checkBox1.setOnCheckedChangeListener(myCheckChange);

        checkBox2.setOnCheckedChangeListener(myCheckChange);

        checkBox3.setOnCheckedChangeListener(myCheckChange);

    }

    class MyCheckChange implements CompoundButton.OnCheckedChangeListener{

        @Override

        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            if(buttonView.getId()==R.id.check1){

                textView.setText(checkBox1.getText());

            }else if(buttonView.getId()==R.id.check2){

                textView.setText(checkBox2.getText());

            }else if(buttonView.getId()==R.id.check3){

                textView.setText(checkBox3.getText());

            }

        }

    }

}



<?xml version="1.0" encoding="utf-8"?>

<LinearLayout 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:orientation="vertical"

    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <TextView

        android:id="@+id/viewText"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="@string/app_name"/>

    <CheckBox

    android:id="@+id/check1"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="11111"/>

    <CheckBox

        android:id="@+id/check2"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="22222"/>

    <CheckBox

        android:id="@+id/check3"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="333333"/>

</LinearLayout>


发表评论:

Powered By Z-BlogPHP 1.4 Deeplue Build 150101

Copyright Your WebSite.Some Rights Reserved.

蜀ICP备11021721号-5