zoukankan      html  css  js  c++  java
  • 控件checkbox和radiobutton和Toast

    一、             CheckBox控件

    布局文件代码:

    <CheckBoxandroid:id="@+id/read"android:text="阅读"  android:layout_width="fill_parent"

    android:layout_height="wrap_content"/>

    <CheckBoxandroid:id="@+id/game"android:text="游戏"  android:layout_width="fill_parent"

    android:layout_height="wrap_content"/>

    <CheckBoxandroid:id="@+id/movie"android:text="电影"  android:layout_width="fill_parent"

    android:layout_height="wrap_content"/>

    Activity代码:

    //设置监听器

    read=(CheckBox)findViewById(R.id.read);

            read.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {           

               public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                  // TODO Auto-generated method stub

                  if(read.isChecked()){

                      Toast.makeText(CheckDemo.this, "你选择了:"+read.getText(), Toast.LENGTH_LONG).show();

                  }

               }

           });

    二、             RadioButton控件

    RadioButton控件必须位于一个RadioGroup内。

    布局代码如下:

    <RadioGroupandroid:id="@+id/rg" 

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    <RadioButtonandroid:id="@+id/nan"android:text=""android:layout_width="fill_parent"

    android:layout_height="wrap_content"/>

    <RadioButtonandroid:id="@+id/nv"android:text=""android:layout_width="fill_parent"

    android:layout_height="wrap_content"/>

    </RadioGroup>

    Activity代码如下:

    //设置监听器

    rg=(RadioGroup)findViewById(R.id.rg);

            rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {        

               public void onCheckedChanged(RadioGroup group, int checkedId) {

                  // TODO Auto-generated method stub

                  if(nan.getId()==checkedId){

                      Toast.makeText(CheckDemo.this, "你选择了:"+nan.getText().toString(), Toast.LENGTH_SHORT).show();

                  }

                  if(nv.getId()==checkedId){

                      Toast.makeText(CheckDemo.this, "你选择了:"+nv.getText().toString(), Toast.LENGTH_SHORT).show();

                  }

               }

           });

    三、             Toast的用法

    Toast.makeText(context,stringInfo,Toast. LISTEN_LONG).show();

    Context指明了当前上下文,一般是当前activity的类

  • 相关阅读:
    Python 使用 environs 库来更好地定义环境变量
    五分钟带你了解map、reduce和filter
    部署React前端和Django后端的3种方法
    marshmallow库更优雅的数据转换
    利用 attrs 和 cattrs 两个库实现 Python 面向对象编程
    parse库,更友好的格式化数据提取方案
    python之prettytable模块格式化打印
    使用类型注解让 Python 代码更易读
    jksj算法训练营-第三课01 数组、链表、跳表
    linux安装java步骤
  • 原文地址:https://www.cnblogs.com/itfenqing/p/4429532.html
Copyright © 2011-2022 走看看