关于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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 正则表达式匹配任意字符(包括换行符)[通俗易懂]

    正则表达式匹配任意字符(包括换行符)[通俗易懂]可以用([\s\S]*),也可以用“([\d\D]*)”、“([\w\W]*)”来匹配,就可以匹配包括换行符在内的任意字符。http://tools.jb51.net/regex/javas

    2022年7月1日
    31
  • aws亚马逊配置 https,操作步骤记录[通俗易懂]

    aws亚马逊配置 https,操作步骤记录

    2022年2月14日
    36
  • shell中if elif_shell编程if语句格式

    shell中if elif_shell编程if语句格式测试shell脚本编程时,写了如下代码:在对if-elif-else分支进行数值判断时,发现一个奇怪的现象:如果使用testconditon(即[condition])进行判定,当第一条if条件为假时,无论代码中的elif语句条件是否为真,都输出elif分支下的语句;查看输出结果,发现输出结果显然与期望值不一样为了能够得到预期结果,发现如果采用双圆括号是进行判断,可得到预期结…

    2022年8月18日
    4
  • 毕业五年

    好久不见,一年一度的“毕业N年”系列,2020,毕业五年了,今年来的略晚一些。五年是个挺重要的节点,所以今年不只是第五年,也是对自己前五年的复盘和总结。工作1、机会:能力很重要,机会最重要珍惜工作中脱颖而出的机会,不要轻易跳槽,要跳,一定是因为看到更好的机会。从我的经历来讲,毕业五年,加上实习已经工作了六年时间,百度(深圳)->百度(北京)->Finger(杭州)->阿里(杭州),一共换了四个团队,但都是个人原因,而不是因为更好的机会。这就导致换了几个队伍,好位置早已经有人,只能

    2022年3月11日
    43
  • printwriter Java,java PrintWriter无法解析

    printwriter Java,java PrintWriter无法解析IhavenoideawhyIgetthemessage”cannotberesolved”onoutineclipseonthe11thlineimportjava.io.*;publicclassdriver{publicstaticvoidmain(String[]args){try{PrintWriterout=newPri…

    2022年8月10日
    5
  • 记录一次mysql 主从不同步的问题操作(hashlinux)

    记录一次mysql 主从不同步的问题操作(hashlinux)

    2021年9月16日
    48

发表回复

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

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