Android API 中文(13) —— ToggleButton

前言

  关键字: Android API 中文,Android 中文 API,android sdk 中文

  本章翻译的是android.widget.ToggleButton,译为开关按钮 。欢迎更多朋友一起参与Android API 的中文翻译行动!再次感谢移动社区提供的积分支持!我的邮箱over140@gmail.com。

声明

  欢迎转载,但请保留文章原始出处:)

    博客园:http://www.cnblogs.com

    农民伯伯: http://www.cnblogs.com/over140/

版本

  Android 2.2 r1

正文

  一、结构

    public class ToggleButton extends CompoundButton

    java.lang.Object

     android.view.View

     android.widget.TextView

     android.widget.Button

     android.widget.CompoundButton

     android.widget.ToggleButton

  二、 类概述

    ToggleButton例子

    通过一个带有亮度指示同时默认文本为“ON”或“OFF”的按钮显示选中/未选中状态。

  三、XML属性

属性名称

描述

android:disabledAlpha

设置按钮在禁用时透明度。

ToggleButton例子

android:textOff

未选中时按钮的文本

android:textOn

选中时按钮的文本

  四、公共方法

public CharSequence getTextOff ()

返回按钮未选中时的文本。

返回值

文本

public CharSequence getTextOn ()

返回按钮选中时的文本。

返回值

文本

public void setBackgroundDrawable (Drawable d)

设置指定的可绘制(译者注:如图片)为背景,或删除背景。如果让背景有边距,这个视图的边距就是背景的边距。然而,当背景被删除时,这个视图的边距不能被触摸。如果需要设置边距,请使用方法setPadding(int, int, int, int)

(译者注:如果设置删除背景整个就不显示了,此外设置背景后选中和被选中的图片也不显示了,如下图:ToggleButton例子 ,实现代码:

ToggleButton例子

参数

d 设置可绘制(译者注:如图片)为背景,或设置为空删除背景。

public void setChecked (boolean checked)

改变按钮的选中状态。

参数

checked true让按钮选中,false让按钮不选中

public void setTextOff (CharSequence textOff)

设置按钮未选中时显示的文本。

参数

textOff 文本

public void setTextOn (CharSequence textOn)

设置按钮选中时显示的文本。

参数

textOn 文本

  五、受保护方法

protected void drawableStateChanged ()

在视图状态的变化影响到所显示可绘制的状态时调用这个方法。

确保在覆盖时中调用父类方法(译者注:super. drawableStateChanged ())。

protected void onFinishInflate ()

XML文件加载视图完成时调用。这个函数在加载的最后阶段被调用,所有的子视图已经被添加。

即使子类重写了onFinishInflate方法,也应该始终确保调用父类方法(译者注:super. onFinishInflate()),使系统能够调用。

  六、下载

    http://download.csdn.net/source/2746654

  八、系列

    Android 2.2 API 中文文档系列(1) —— TextView

    Android 2.2 API 中文文档系列(2) —— EditText

    Android 2.2 API 中文文档系列(3) —— AccessibilityService

    Android 2.2 API 中文文档系列(4) —— Manifest

    Android 2.2 API 中文文档系列(5) —— View

    Android 2.2 API 中文文档系列(6) —— ImageView

    Android 2.2 API 中文文档系列(7) —— ImageButton

     Android 2.2 API 中文文档系列(8) —— QuickContactBadge

    Android 2.2 API 中文文档系列(9) —— ZoomButton

     Android 2.2 r1 API 中文文档系列(10) —— CheckBox

    Android 2.2 r1 API 中文文档系列(11) —— RadioButton

    Android 2.2 r1 API 中文文档系列(12) —— Button

例子1

ToggleButton的状态只能是选中和未选中,并且需要为不同的状态设置不同的显示文本。

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/bulb_off"
android:layout_gravity="center_horizontal"
/>
<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="140dip"
android:layout_height="wrap_content"
android:textOn="开灯"
android:textOff="关灯"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>

Activity

import android.app.Activity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ToggleButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class MyToggleButtonActivity extends Activity {
private ImageView imageView=null;
private ToggleButton toggleButton=null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView=(ImageView) findViewById(R.id.imageView);
toggleButton=(ToggleButton)findViewById(R.id.toggleButton);
toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
toggleButton.setChecked(isChecked);
imageView.setImageResource(isChecked?R.drawable.bulb_on:R.drawable.bulb_off);
}
});
}
}

运行效果:

ToggleButton例子 - 夏天的风 - FreeSimpleHappy

ToggleButton例子 - 夏天的风 - FreeSimpleHappy

例子2

android开发 ToggleButton开发使用方法ToggleButton是一种带状态的Button,有ON,或OFF状态


XML代码:

Xml代码 复制代码 收藏代码
  1. <ToggleButtonandroid:id="@+id/tb"
  2. android:layout_width="wrap_content"
  3. android:layout_height="wrap_content"
  4. android:textOn="开"
  5. android:textOff="关"
  6. android:checked="true"
  7. />

<ToggleButton android:id="@+id/tb" android:layout_width="wrap_content" android:layout_height="wrap_content"android:textOn="开"android:textOff="关"android:checked="true" />



textOn 按钮开启时显示的文本
textOff 按钮关闭时显示的文本
checked 载入时的状态,默认为false,即关
JAVA代码

Java代码 复制代码 收藏代码
    1. importandroid.app.Activity;
    2. importandroid.os.Bundle;
    3. importandroid.widget.CompoundButton;
    4. importandroid.widget.Toast;
    5. importandroid.widget.ToggleButton;
    6. importandroid.widget.CompoundButton.OnCheckedChangeListener;
    7. publicclassMainextendsActivity{
    8. /**Calledwhentheactivityisfirstcreated.*/
    9. @Override
    10. publicvoidonCreate(BundlesavedInstanceState){
    11. super.onCreate(savedInstanceState);
    12. setContentView(R.layout.main);
    13. ToggleButtontb=(ToggleButton)findViewById(R.id.tb);
    14. tb.setOnCheckedChangeListener(newOnCheckedChangeListener(){
    15. @Override
    16. publicvoidonCheckedChanged(CompoundButtonbuttonView,
    17. booleanisChecked){
    18. //TODOAuto-generatedmethodstub
    19. //isChecked就是按钮状态
    20. if(isChecked){
    21. Toast.makeText(Main.this,"打开",Toast.LENGTH_LONG).show();
    22. }else{
    23. Toast.makeText(Main.this,"关闭",Toast.LENGTH_LONG).show();
    24. }
    25. }
    26. });
    27. }
    28. }

更多相关文章

  1. Android按钮控件之RadioGroup和RadioButton
  2. 安卓布局属性代码中文注解
  3. 【Android常用控件】EditText常用属性【二】:为文本输入框指定软
  4. android 开发技巧(9)--为文本添加发亮的效果
  5. android单选按钮RadioGroup的详细使用
  6. Android API中文文档GridView
  7. Android Studio点击按钮更换背景图片
  8. Titanium中Android模块开发指南(中文)

随机推荐

  1. android修改默认语言
  2. 解耦问题
  3. Android(安卓)PlayGame
  4. Android(安卓)-- App生成快捷方式
  5. Android(安卓)DownloadManager 使用
  6. Android当前时间的获取
  7. android AudioManager
  8. Android(安卓)双击退出应用
  9. Android(安卓)Studio 无法预览xml布局视
  10. 解决Could not find method android() fo