zoukankan      html  css  js  c++  java
  • 第19讲- UI组件之_Button、checkbox、radio

    19讲 UI组件之_Buttoncheckboxradio

    四、按钮Button

    Button继承自TextView,间接继承自View。当用户对按钮进行操作的时候,触发相应事件,如点击,触摸。

    1ImageButton

    ImageButton继承自Button,可以在ImageButton中显示一个图片展示给用户看,并且对其Text属性设置值的时候是无效的,其它功能与Button一样。

        <ImageButton

           android:id="@+id/imageButton1"

            android:layout_width="wrap_content"

           android:layout_height="wrap_content"

           android:src="@drawable/ic_launcher" />

     

    为ImageButton添加监听器注册事件

    方式1:通过onClickListener

    imgBtn01.setOnClickListener(newOnClickListener() {

            public void onClick(View v) {   myTextView.setText("ImageButton的监听事件");  }  

    }); 

    方式2:通过XML文件设置

    <ImageButtonandroid:id="@+id/imgBtn02"        android:src="@drawable/back_48"    

         android:layout_width="wrap_content"     android:layout_height="wrap_content"    

          android:onClick="ImageButtonXml"/> 

    2ToggleButton 开关按钮

    ToggleButton,一个开关按钮,有两个状态,可以通过两个属性显示不同状态时,控件内显示文字的内容不同,属性如下:

    android:textOff/setTextOff(CharSequence):设置关闭时显示的文本内容。

    android:textOn/setTextOn(CharSequence):设置打开时显示的文本内容。

    android:textOn="男"

    android:textOff="女"

    android:onClick="test"

             publicvoid test(View view){

                       ToggleButtonbutton = (ToggleButton)view;

                       Toast.makeText(this,button.getText(), 0).show();

             }

    3RadioButton(单选按钮)  RadioGroup(单选框)

    RadioButton,为一个单选按钮,一般配合RadioGroup一起使用,在同一RadioGroup内,所有的RadioButton的选中状态为互斥,它们有且只有一个RadioButton被选中。

    常用方法如下:

    RadioGroup.check(intid);                            //将指定的RadioButton设置成选中状态。

    (RadioButton)findViewById(radioGroup.getCheckedRadioButtonId());         //获取被选中的单选框。

    RadioButton.getText();                                //获取单选框的值

    获取点击RadioButton的信息

    RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radioGroup1);

    radioGroup.setOnCheckedChangeListener(newOnCheckedChangeListener() {

    public void onCheckedChanged(RadioGroup group, int checkedId) {

    RadioButton button=(RadioButton) group.findViewById(checkedId);

    Toast.makeText(getApplicationContext(), button.getText(),Toast.LENGTH_LONG).show();

    //Toast.makeText(getApplicationContext(), (String)button.getTag(),1).show();

    }

    });

    button类有两个重要属性:text和tag。前者是显示给用户看的,而后者tag是button的标签,是用户不可见的,作为编写程序时利用,用于数据存储与传输。

    3CheckBox多选框

    CheckBox是一个多选按钮,每个多选框都是独立的,所以也无需用一个组控件包裹起来。可以通过迭代所有多选框,然后根据其状态是否被选中再获取其值。

    CheckBox 类常用方法:

    CheckBox.setChecked(true);           //将CheckBox 设置成选中状态。

    CheckBox.getText();                          //获取多选框的值

    CheckBox.isChecked();                   //判断该选项是否被选中,如果选中返回true

    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);

    private OnCheckedChangeListener  myListener = new OnCheckedChangeListener() {

    public voidonCheckedChanged(CompoundButton arg0, boolean arg1) {

    if(arg1) {

    Toast.makeText(getApplicationContext(), arg0.getText()+"被选中了!",0).show();

    }else {

    Toast.makeText(getApplicationContext(), arg0.getText()+"被取消了!",0).show();

    }

    }

    };

  • 相关阅读:
    Response 文件下载
    Tomcat Servlet
    Junit 反射 注解
    Stream流 方法引用
    函数式接口
    网络编程
    缓冲流、转换流、序列化流、打印流
    字节流 字符流
    File类 递归
    线程池 Lambda表达式
  • 原文地址:https://www.cnblogs.com/anyuan9/p/6171602.html
Copyright © 2011-2022 走看看