zoukankan      html  css  js  c++  java
  • 19 UI_常用组件-BasicViews-button-checkbox-radio

    UI_常用组件-BasicViews-button-checkbox-radio


    1、关于Button的演示案例:

    main.xml: 

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
    
        <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />
    
        <ToggleButton
            android:id="@+id/toggleButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textOn="男"
            android:textOff="女"
            android:onClick="test"/>
    
    </LinearLayout>

    Test_buttonActivity.java:

    package test.button;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Toast;
    import android.widget.ToggleButton;
    
    public class Test_buttonActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
        }
        
        public void test(View view){
            ToggleButton toggleButton =(ToggleButton) view;
            Toast.makeText(this, toggleButton.getText(), 0).show();
        }
    }

    运行结果(点击ToggleButton)

       


    2、关于RadioGroup单选按钮的演示案例:

    main.xml: 

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
        <RadioButton
            android:id="@+id/radioButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="RadioButton" />
    
        <RadioGroup
            android:id="@+id/radioGroup1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
    
            <RadioButton
                android:id="@+id/radio0"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:text="语文" 
                android:tag="1"/>
    
            <RadioButton
                android:id="@+id/radio1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="数学" 
                android:tag="2"/>
    
            <RadioButton
                android:id="@+id/radio2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="英语" 
                android:tag="3"/>
    
        </RadioGroup>
    
    </LinearLayout>

    Test_buttonActivity.java:

    package test.button;
    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.RadioButton;
    import android.widget.RadioGroup;
    import android.widget.RadioGroup.OnCheckedChangeListener;
    import android.widget.Toast;
    
    public class Test_buttonActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radioGroup1);
            radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    RadioButton button = (RadioButton)group.findViewById(checkedId);
                    Toast.makeText(getApplicationContext(),(String)button.getTag()+ button.getText(), 0).show();
                }
            });
        }    
    }

    运行结果(点击)

         


    3、关于CheckBox复选框的演示案例:

    main.xml: 

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
        <CheckBox
            android:id="@+id/checkBox1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="语文" 
            android:tag="1"/>
    
        <CheckBox
            android:id="@+id/checkBox2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="数学" 
            android:tag="2"/>
    
        <CheckBox
            android:id="@+id/checkBox3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="英语" 
            android:tag="3"/>
      
    </LinearLayout>

    Test_buttonActivity.java:

    package test.button;
    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.CheckBox;
    import android.widget.CompoundButton;
    import android.widget.CompoundButton.*;
    import android.widget.Toast;
    public class Test_buttonActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            
            CheckBox box1=(CheckBox) findViewById(R.id.checkBox1);
            CheckBox box2=(CheckBox) findViewById(R.id.checkBox2);
            CheckBox box3=(CheckBox) findViewById(R.id.checkBox3);
            box1.setOnCheckedChangeListener(myListener);
            box2.setOnCheckedChangeListener(myListener);
            box3.setOnCheckedChangeListener(myListener);
            box1.setOnCheckedChangeListener(myListener);        
        }
        private OnCheckedChangeListener myListener = new OnCheckedChangeListener(){
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    Toast.makeText(getApplicationContext(), buttonView.getText()+"被选中了", 0).show();
                }else{
                    Toast.makeText(getApplicationContext(), buttonView.getText()+"被取消了", 0).show();
                }
                
            }
        };
        
    }

    运行结果(点击)

      

  • 相关阅读:
    Linux 下建立 Git 与 GitHub 的连接
    PHP Redis 对象方法手册
    wampServer 安装 Redis 扩展
    CentOS 与 Ubuntu 使用命令搭建 LAMP 环境
    Xshell 连接 CentOS 7 与 Ubuntu Server
    使用那各VUE的打印功能(print.js)出现多打印一个空白页的问题
    MySql数据库,查询数据导出时会出现重复的记录(数据越多越明显)
    解决 nginx 启动错误 nginx: [emerg] host not found in upstream
    关于nginx配置的一个报错connect() to unix:/tmp/php-cgi.sock failed (2: No such file or directory)
    网站的ssl证书即将过期,需要续费证书并更新
  • 原文地址:https://www.cnblogs.com/cxm-weiniss/p/7216351.html
Copyright © 2011-2022 走看看