关于Android多按钮切换的例子!

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

res/values/strings.xml文件:

 
 
  1. <?xml version="1.0" encoding="utf-8"?><resources>
  2.     <string name="app_name">MyAndroidApp</string>
  3.     <string name="toggle_turn_on">Turn On</string>
  4.     <string name="toggle_turn_off">Turn Off</string>
  5.     <string name="btn_display">Display</string></resources>

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

文件:res/layout/ main.xml

 
 
  1. <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     android:layout_width="fill_parent"
  3.     android:layout_height="fill_parent"
  4.     android:orientation="vertical" >
  5.  
  6.     <ToggleButton
  7.         android:id="@+id/toggleButton1"
  8.         android:layout_width="wrap_content"
  9.         android:layout_height="wrap_content"
  10.         android:text="ToggleButton" />
  11.  
  12.     <ToggleButton
  13.         android:id="@+id/toggleButton2"
  14.         android:layout_width="wrap_content"
  15.         android:layout_height="wrap_content"
  16.         android:textOn="@string/toggle_turn_on"
  17.         android:textOff="@string/toggle_turn_off"
  18.         android:checked="true" />
  19.  
  20.     <Button
  21.         android:id="@+id/btnDisplay"
  22.         android:layout_width="wrap_content"
  23.         android:layout_height="wrap_content"
  24.         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

 
 
  1. 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
  2.   public void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);
  3.     setContentView(R.layout.main);
  4.  
  5.     addListenerOnButton();
  6.  
  7.   }  public void addListenerOnButton() {
  8.  
  9.     toggleButton1 = (ToggleButton) findViewById(R.id.toggleButton1);
  10.     toggleButton2 = (ToggleButton) findViewById(R.id.toggleButton2);
  11.     btnDisplay = (Button) findViewById(R.id.btnDisplay);
  12.  
  13.     btnDisplay.setOnClickListener(new OnClickListener() {        @Override
  14.         public void onClick(View v) {
  15.  
  16.            StringBuffer result = new StringBuffer();
  17.            result.append("toggleButton1 : ").append(toggleButton1.getText());
  18.            result.append("ntoggleButton2 : ").append(toggleButton2.getText());
  19.  
  20.            Toast.makeText(MyAndroidAppActivity.this, result.toString(),
  21.             Toast.LENGTH_SHORT).show();
  22.  
  23.         }
  24.  
  25.     });
  26.  
  27.   }
  28. }
  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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值