• 常用控件 二


    这里我介绍一下RadioGroup和RadioButton ,CheckBox,Toast 的用法

    Toast就是小的提示信息的浮动框,过一段时间会消失的那种,在市面上应用还是比较多的。

    需要提一下的是今天我在调试代码的时候IDE老是报错,经过反复查看代码都没有查出有任何问题,最后在查看布局文件时采用了绘图布局窗口查看时,看到了明显的XML文件的错误提示消息,这个技巧在以后编写代码时应该反复使用,避免这种不必要的错误

     1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     android:layout_width="fill_parent"
     3     android:layout_height="fill_parent"
     4     android:orientation="vertical" >
     5 
     6     <TextView
     7         android:id="@+id/textView1"
     8         android:layout_width="fill_parent"
     9         android:layout_height="wrap_content"
    10         android:text="hello" />
    11 
    12     <RadioGroup
    13         android:id="@+id/genderGroup"
    14         android:layout_width="wrap_content"
    15         android:layout_height="wrap_content"
    16         android:orientation="vertical" >
    17 
    18         <RadioButton
    19             android:id="@+id/female"
    20             android:layout_width="wrap_content"
    21             android:layout_height="wrap_content"
    22             android:text="female" />
    23 
    24         <RadioButton
    25             android:id="@+id/male"
    26             android:layout_width="wrap_content"
    27             android:layout_height="wrap_content"
    28             android:text="male" />
    29     </RadioGroup>
    30 
    31     <CheckBox
    32         android:id="@+id/swim"
    33         android:layout_width="wrap_content"
    34         android:layout_height="wrap_content"
    35         android:text="swim" />
    36 
    37     <CheckBox
    38         android:id="@+id/run"
    39         android:layout_width="wrap_content"
    40         android:layout_height="wrap_content"
    41         android:text="run" />
    42 
    43     <CheckBox
    44         android:id="@+id/read"
    45         android:layout_width="wrap_content"
    46         android:layout_height="wrap_content"
    47         android:text="read" />
    48 
    49 </LinearLayout>
     1 package com.example.helloworld;
     2 
     3 import android.app.Activity;
     4 import android.os.Bundle;
     5 import android.widget.CheckBox;
     6 import android.widget.CompoundButton;
     7 import android.widget.RadioButton;
     8 import android.widget.RadioGroup;
     9 import android.widget.Toast;
    10 import android.widget.RadioGroup.OnCheckedChangeListener;
    11 
    12 /**
    13  * 常用控件 二 RadioGroup和RadioButton ,CheckBox,Toast(弹出的提示信息)
    14  * 
    15  * @author David
    16  * 
    17  */
    18 public class NineLessonDemo1 extends Activity {
    19 
    20     private RadioGroup genderGroup = null;
    21     private RadioButton femaleButton = null;
    22     private RadioButton maleButton = null;
    23     private CheckBox swimBox = null;
    24     private CheckBox runBox = null;
    25     private CheckBox readBox = null;
    26 
    27     @Override
    28     protected void onCreate(Bundle savedInstanceState) {
    29         // TODO Auto-generated method stub
    30         super.onCreate(savedInstanceState);
    31         setContentView(R.layout.nine);
    32         genderGroup = (RadioGroup) this.findViewById(R.id.genderGroup);
    33         femaleButton = (RadioButton) this.findViewById(R.id.female);
    34         maleButton = (RadioButton) this.findViewById(R.id.male);
    35         swimBox = (CheckBox) this.findViewById(R.id.swim);
    36         runBox = (CheckBox) this.findViewById(R.id.run);
    37         readBox = (CheckBox) this.findViewById(R.id.read);
    38         genderGroup.setOnCheckedChangeListener(new MyRadioButtonListener());
    39         // checkbox需要为每一个这样的对象分别添加一次
    40         swimBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    41 
    42             public void onCheckedChanged(CompoundButton buttonView,
    43                     boolean isChecked) {
    44                 // TODO Auto-generated method stub
    45                 if (isChecked) {
    46                     System.out.println("swim is checked");
    47 
    48                 } else {
    49 
    50                     System.out.println("swim is unchecked");
    51                 }
    52             }
    53         });
    54         runBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    55 
    56             public void onCheckedChanged(CompoundButton buttonView,
    57                     boolean isChecked) {
    58                 // TODO Auto-generated method stub
    59                 if (isChecked) {
    60                     System.out.println("run is checked");
    61 
    62                 } else {
    63 
    64                     System.out.println("run is unchecked");
    65                 }
    66             }
    67         });
    68         readBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    69 
    70             public void onCheckedChanged(CompoundButton buttonView,
    71                     boolean isChecked) {
    72                 // TODO Auto-generated method stub
    73                 if (isChecked) {
    74                     System.out.println("read is checked");
    75                 } else {
    76                     System.out.println("read is unchecked");
    77                 }
    78             }
    79         });
    80     }
    81 
    82     class MyRadioButtonListener implements OnCheckedChangeListener {
    83 
    84         public void onCheckedChanged(RadioGroup arg0, int arg1) {
    85             // TODO Auto-generated method stub
    86             if (femaleButton.getId() == arg1) {
    87                 System.out.println("female");
    88                 Toast.makeText(NineLessonDemo1.this, "female",
    89                         Toast.LENGTH_SHORT).show();
    90             } else {
    91                 System.out.println("male");
    92 
    93             }
    94         }
    95 
    96     }
    97 
    98 }

    --------------------------------------------------------------------------------------------------------------------------------------------
    顺势而为
  • 相关阅读:
    Need to know which polygon shell contains a given face
    静默安装 Microsoft Visual C++ 运行库
    python linecache模块 读取文件行使用注意事项
    不运行 maya 就可以获取 maya 版本,在 python 中获取
    linux setfacl chmod g+s 修改文件夹和文件的默认访问权限和所属组
    Maya material & shading groups 相互获取对应的彼此
    PyQt PySide 查询内置可用的 style
    maxscript 在长时间的运算中卡主,白屏的解决方法 windows.processPostedMessages()
    QProcessBar setFormat 设置进度格式
    MaxPlus WStr Python 中的字符串传递给 MaxPlus
  • 原文地址:https://www.cnblogs.com/zhuzhenyu/p/2614481.html
Copyright © 2020-2023  润新知