zoukankan      html  css  js  c++  java
  • anroid的checkbox

    1.layout代码

     1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     xmlns:tools="http://schemas.android.com/tools"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     android:paddingBottom="@dimen/activity_vertical_margin"
     6     android:paddingLeft="@dimen/activity_horizontal_margin"
     7     android:paddingRight="@dimen/activity_horizontal_margin"
     8     android:paddingTop="@dimen/activity_vertical_margin"
     9     tools:context=".MainActivity" >
    10 
    11     <CheckBox
    12         android:id="@+id/checkBox1"
    13         android:layout_width="match_parent"
    14         android:layout_height="wrap_content"
    15         android:layout_alignParentTop="true"
    16         android:layout_centerHorizontal="true"
    17         android:layout_marginTop="17dp"
    18         android:text="篮球" />
    19 
    20     <CheckBox
    21         android:id="@+id/checkBox2"
    22         android:layout_width="match_parent"
    23         android:layout_height="wrap_content"
    24         android:layout_alignLeft="@+id/checkBox1"
    25         android:layout_below="@+id/checkBox1"
    26         android:text="足球" />
    27 
    28     <CheckBox
    29         android:id="@+id/checkBox3"
    30         android:layout_width="match_parent"
    31         android:layout_height="wrap_content"
    32         android:layout_alignLeft="@+id/checkBox2"
    33         android:layout_below="@+id/checkBox2"
    34         android:text="乒乓球" />
    35 
    36 </RelativeLayout>

    2.源代码

    package com.example.text2;
    
    import android.os.Bundle;
    import android.util.Log;
    import android.accounts.Account;
    import android.accounts.OnAccountsUpdateListener;
    import android.app.Activity;
    import android.view.Menu;
    import android.widget.CheckBox;
    import android.widget.CompoundButton;
    import android.widget.CompoundButton.OnCheckedChangeListener;
    
    public class MainActivity extends Activity {
        private CheckBox CheckBox1;
        private CheckBox CheckBox2;
        private CheckBox CheckBox3;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            CheckBox1 =(CheckBox) findViewById(R.id.checkBox1);
            CheckBox2 =(CheckBox) findViewById(R.id.checkBox2);
            CheckBox3 =(CheckBox) findViewById(R.id.checkBox3);
            
            CheckBox1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                
                @Override
                public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                    // TODO Auto-generated method stub
                    if(arg1){
                        String text = CheckBox1.getText().toString();
                        Log.i("tag", text);
                    }
                }
            });
            
            CheckBox2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                
                @Override
                public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                    // TODO Auto-generated method stub
                    if (arg1) {
                        String text = CheckBox2.getText().toString();
                        Log.i("tag",text);
                    }
                    
                }
            });
            
            CheckBox3.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                
                @Override
                public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                    // TODO Auto-generated method stub
                    if(arg1){
                        String text = CheckBox3.getText().toString();
                        Log.i("tag", text);
                    }
                }
            });
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    
    }

    3.点击足球选项之后,logcat的效果

    04-18 22:37:19.921: I/tag(1694): 足球

    本博客基于网络课程完成,旨在学习,有错误请指正!
  • 相关阅读:
    创建react项目
    解决移动端弹窗下页面滚动问题
    前端常用的几种加密方式
    http请求状态码
    vue代理配置
    自动化测试实操案例详解 | Windows应用篇
    Google 再见 Java
    一次诡异的 SQL 数量统计查询不准的问题
    Maven
    淘宝技术分享:手淘亿级移动端接入层网关的技术演进之路
  • 原文地址:https://www.cnblogs.com/comefuture/p/6730985.html
Copyright © 2011-2022 走看看