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


相关推荐

  • ubuntu安装python pip_anaconda pipenv

    ubuntu安装python pip_anaconda pipenv简单介绍在Ubuntu上安装Python的virtualenv前提:本机安装了Linux系统(这里推荐用Ubuntu安装,或是CentOS)(Ubuntu的安装比较简单,推荐用VirtualBox,可以去Oracle官网下载VirtualBox(https://www.virtualbox.org/),然后去Ubuntu官网(https://www.ubuntu.com/download)…

    2022年8月26日
    4
  • unity调用animation_高跟鞋踩坑

    unity调用animation_高跟鞋踩坑Unity小科普老规矩,先介绍一下Unity的科普小知识:Unity是实时3D互动内容创作和运营平台。包括游戏开发、美术、建筑、汽车设计、影视在内的所有创作者,借助Unity将创意变成现实。Unity平台提供一整套完善的软件解决方案,可用于创作、运营和变现任何实时互动的2D和3D内容,支持平台包括手机、平板电脑、PC、游戏主机、增强现实和虚拟现实设备。也可以简单把Unity理解为一个游戏引擎,可以用来专业制作游戏!Unity踩坑小知识点学习Unity使用Q..

    2025年11月6日
    3
  • 服务器硬件基础知识

    服务器硬件基础知识服务器的概述计算机的硬件主要有主机和输入/输出设备。主机包括机箱,电源,主板,CPU(中央处理器),内存,显卡,声卡,网卡,硬盘,光驱等。输入/输出包括显示器,键盘,鼠标,音箱,摄像头,打印机和扫描仪等。服务器服务器是指在网络环境下运行相应的应用软件,为网上用户提供共享信息资源和各种服务的一直高性能计算机。服务器的选择:处理器性能,I/O性能,管理性,可靠性,扩展性。同样…

    2022年7月22日
    11
  • java防止接口重复请求_前端防止重复提交

    java防止接口重复请求_前端防止重复提交PopularMVC框架接口防重复提交功能使用示例简介1、简介此项目用于演示如何使用popularmvc提供的接口防重复提交功能。使用防重提交功能,只需要在需要防重的接口上添加@RequiredNoRepeatSubmit注解即可。主要有以下内容:防重复提交码模式自定义防重复提交码自定义防重复提交码需要调用者保证防重复提交码的全局唯一性,推荐结构:userId+timestamptimestamp在数据更新后才允许更新使用sign作为防重码如果接口开启了数字签

    2022年9月1日
    4
  • Java开发手册之OOP规约

    Java开发手册之OOP规约Java开发手册之OOP规约

    2022年4月22日
    45
  • Symantec赛门铁克安全软件免密卸载方式[通俗易懂]

    Symantec赛门铁克安全软件免密卸载方式[通俗易懂]装了Symantec后,后面希望卸载他,结果发现卸载需要卸载口令,查了一堆资料,总结有如下几种:1、卸载口令可能是symantec,反正没成本可以简单试试看。不过我是没有通过,这个口令不对我的Symantec。2、使用cleanwipe进行卸载,这是官方的用于卸载Symantec软件的工具。工具很小,应该有版本要求,旧版的不能完成卸载。推荐使用这个方式。我用的是CleanWipe_14.3.558.1000,选中下图中框出来的三个勾,直接下一步即可完成卸载。链接:https://pan.baidu.

    2022年5月1日
    191

发表回复

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

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