大家好,又见面了,我是你们的朋友全栈君。
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:android="http://schemas.android.com/apk/res/android" 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:id="@+id/imageView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:scaleType="fitXY" android:src="@drawable/btn" /> <ToggleButton android:id="@+id/toggleButton1" 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
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/141708.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...