Chinaunix首页 | 论坛 | 博客
  • 博客访问: 144557
  • 博文数量: 47
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 547
  • 用 户 组: 普通用户
  • 注册时间: 2014-09-01 08:57
个人简介

一个人的生活 新奇视 新感觉 www.x74.cn

文章分类

全部博文(47)

文章存档

2018年(20)

2017年(25)

2015年(1)

2014年(1)

我的朋友

分类: Android平台

2017-12-26 17:08:57

1。自定义字符串
Open “res/values/strings.xml” file, add some custom string for toggle buttons.

res/values/strings.xml文件:

xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">MyAndroidAppstring> <string name="toggle_turn_on">Turn Onstring> <string name="toggle_turn_off">Turn Offstring> <string name="btn_display">Displaystring> resources>

2。切换按钮
Open “res/layout/ main.xml” file, add two “切换按钮” and a normal button, inside the 线性布局.

文件:res/layout/ main.xml

xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ToggleButton android:id="@+id/toggleButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="ToggleButton" /> <ToggleButton android:id="@+id/toggleButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOn="@string/toggle_turn_on" android:textOff="@string/toggle_turn_off" android:checked="true" /> <Button android:id="@+id/btnDisplay" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/btn_display" /> LinearLayout>

笔记
Review the “togglebutton2”, we did customized the togglebutton2’s display text on and off and made it checked by default.

三.代码代码
Inside activity “onCreate()” method, attach a click listeners on a normal button, to display the current state of the toggle button.

文件:myandroidappactivity.java

package com.mkyong.android;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import android.widget.ToggleButton; public class MyAndroidAppActivity extends Activity {

  private ToggleButton toggleButton1, toggleButton2;
  private Button btnDisplay; @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    addListenerOnButton();

  }

  public void addListenerOnButton() {

    toggleButton1 = (ToggleButton) findViewById(R.id.toggleButton1);
    toggleButton2 = (ToggleButton) findViewById(R.id.toggleButton2);
    btnDisplay = (Button) findViewById(R.id.btnDisplay); btnDisplay.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) {

           StringBuffer result = new StringBuffer();
           result.append("toggleButton1 : ").append(toggleButton1.getText());
           result.append("\ntoggleButton2 : ").append(toggleButton2.getText());

           Toast.makeText(MyAndroidAppActivity.this, result.toString(),
            Toast.LENGTH_SHORT).show();

        }

    });

  }
}
  1. Demo
    Run the application.

  2. Result, toggleButton2 is using the customized string, and checked by default.

图片描述


android togglebutton demo1

  1. Checked toggleButton1 and unchecked toggleButton2, and click on the display button, the current state of both toggle buttons will be displayed.

图片描述
android togglebutton demo2

 

原文博客地址:http://www.apkbus.com/blog-919651-76749.html

阅读(1929) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~