compoundbutton(安卓天天练练CompoundButton[通俗易懂])

发布时间:2025-12-10 19:31:23 浏览次数:5

安卓天天练练CompoundButton[通俗易懂]-

安卓天天练练(五)CompoundButton[通俗易懂]ToggleButton让我想起了从前jQuery还没有取消toggle方法时是怎么偷懒的。。注意:如果LinearLayout,与RelativeLayout不同,必须有orientation。用可视化顶部的横着隔开或者竖着隔开的方形按钮也可以选择,例如android:orientation=”vertical”三目运算符前面和js一样,那个state是不需要额外带括号的按…

ToggleButton让我想起了从前jQuery还没有取消toggle方法时是怎么偷懒的。。

注意:

如果LinearLayout,与RelativeLayout不同,必须有orientation。用可视化顶部的横着隔开或者竖着隔开的方形按钮也可以选择,例如android:orientation=“vertical”三目运算符前面和js一样,那个state是不需要额外带括号的按钮右键点上去有Edit TextOff,TextOn文字右键点上去的Edit Text可以新建string,填好String和R.string.之后自动生成string.xml到/values图片右键点上去有ScaleType可供选择如果AVD启动黑屏,又没有报错,尝试删除旧的AVD新建后start再run as
package com.example.android_8_1;import android.app.Activity;import android.os.Bundle;import android.widget.CompoundButton;import android.widget.CompoundButton.OnCheckedChangeListener;import android.widget.ImageView;import android.widget.ToggleButton;public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        ToggleButton tg = (ToggleButton) findViewById(R.id.toggleButton1);        tg.setOnCheckedChangeListener(new OnCheckedChangeListener() {            // imported from android.widget.CompoundButton            @Override            public void onCheckedChanged(CompoundButton buttonView,                    boolean isChecked) {                // TODO Auto-generated method stub                setBulbState(isChecked);            }        });    }    public void setBulbState(boolean state) {        ImageView iv = (ImageView) findViewById(R.id.imageView1);        iv.setImageResource(state? R.drawable.btn : R.drawable.btnhover);        ToggleButton tg = (ToggleButton) findViewById(R.id.toggleButton1);        tg.setChecked(state);    }}

XML很简单

<LinearLayout xmlns:andro    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.example.android_8_1.MainActivity" >    <ImageView        android:        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:scaleType="fitXY"        android:src="@drawable/btn" />    <ToggleButton        android:        android:layout_width="80dp"        android:layout_height="40dp"        android:layout_gravity="center_horizontal"        android:text="ToggleButton"        android:textOff="关灯"        android:textOn="开灯" /></LinearLayout>

安卓自带的机制很实用,此例结束。

另外,单选框可以用<RadioGroup></RadioGroup>包起来形成一组,实现单选排斥。

改成一组连动式CompoundButton

package com.example.android_8_1;import com.example.android_8_1.R.id;import android.app.Activity;import android.os.Bundle;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.RadioButton;import android.widget.CompoundButton.OnCheckedChangeListener;import android.widget.ImageView;import android.widget.ToggleButton;public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        ToggleButton tg = (ToggleButton) findViewById(R.id.toggleButton1);        tg.setOnCheckedChangeListener(new OnCheckedChangeListener() {            // imported from android.widget.CompoundButton            @Override            public void onCheckedChanged(CompoundButton buttonView,                    boolean isChecked) {                // TODO Auto-generated method stub                setBulbState(isChecked);            }        });                CheckBox cb = (CheckBox) findViewById(R.id.checkBox1);        cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {            @Override            public void onCheckedChanged(CompoundButton buttonView,                    boolean isChecked) {                // TODO Auto-generated method stub                setBulbState(isChecked);            }        });                RadioButton rb = (RadioButton) findViewById(id.radioButton2);        rb.setOnCheckedChangeListener(new OnCheckedChangeListener() {            @Override            public void onCheckedChanged(CompoundButton buttonView,                    boolean isChecked) {                // TODO Auto-generated method stub                setBulbState(isChecked);            }        });    }    public void setBulbState(boolean state) {        ImageView iv = (ImageView) findViewById(R.id.imageView1);        iv.setImageResource(state ? R.drawable.btn : R.drawable.btnhover);        ToggleButton tg = (ToggleButton) findViewById(R.id.toggleButton1);        CheckBox cb = (CheckBox) findViewById(R.id.checkBox1);                tg.setText(state ? R.string.onn : R.string.offn);        cb.setText(state ? R.string.offn : R.string.onn);        tg.setChecked(state);        cb.setChecked(state);        RadioButton rb = (RadioButton) findViewById(R.id.radioButton2);        rb.setChecked(state);        rb = (RadioButton) findViewById(R.id.radioButton1);        rb.setChecked(!state);    }}

XML不变。需要注意的是,如果逻辑上貌似通顺但是出现checkbox点下去没反应,一定是黑白颠倒弄反鸟!

比如文字设为onn活着offn,checked(!state)在对radioButton1还是radioButton2。

转载于:https://www.cnblogs.com/haimingpro/p/4671239.html

需要做网站?需要网络推广?欢迎咨询客户经理 13272073477