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

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

大家好,又见面了,我是全栈君。

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">MyAndroidApp</string>
    <string name="toggle_turn_on">Turn On</string>
    <string name="toggle_turn_off">Turn Off</string>
    <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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/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

转载于:https://my.oschina.net/u/3724196/blog/1595225

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/107805.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 01_activiti7 学习笔记

    01_activiti7 学习笔记

    2021年7月12日
    83
  • mac ll命令_linux终端命令

    mac ll命令_linux终端命令首先,cd到home下:cd~再编辑.bash_profile文件vim.bash_profile添加如下内容aliasll=’ls-l’#aliasl=’ls-alhF’#aliasla=’ls-AFh’#aliasll=’ls-lhAF’执行source.bash_profile命令,使内容生效source.bash_profi…

    2022年9月24日
    2
  • C语言位运算符_C语言左移和右移的区别

    C语言位运算符_C语言左移和右移的区别如果你想了解以下位运算符的话我想你来对了地方&^|~<<>>首先明确位运算符都是在二进制位上运算的先讲比较简单的<<>>(有些人可能认为这个最难以理解包括我)后来我陡然一时想到了十进制左移“<<”右移“>>”十进制10左移三位就是乘以10的3次方=1000010右…

    2022年10月5日
    2
  • Floyed理解「建议收藏」

    Floyed理解「建议收藏」Floyed理解 Floyd算法的本质是动态规划,其转移方程为:f(k,i,j)=min(f(k-1,i,j),f(k-1,i,k)+f(k-1,k,j))。f(k-1,i,j)表示经过前k-1个点f(k-1,i,k)+f(k-1,k,j)表示经过k这个点f(k,i,j)表示路径除开起点i与终点j,只经过前k个点中的某些点,从i到j的最小值。计算这个值只需要考…

    2022年6月29日
    25
  • 哥哥[通俗易懂]

    哥哥[通俗易懂]哥哥

    2022年4月23日
    37
  • VScode 在 Mac 的快捷键

    VScode 在 Mac 的快捷键control+G快速找到某一行command+shift+k删除整行代码command+fn+delete删除当前行光标后的所有代码command+delete删除当前行光标前的所有代码option+fn+delete删除当前单词光标后到符号之间的代码option+delete删除当前单词光标前到符号之间的代码…

    2022年6月22日
    106

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号