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


相关推荐

  • 光闸和网闸区别_网闸作用

    光闸和网闸区别_网闸作用光闸和网闸区别光闸和网闸区别光闸和网闸区别

    2022年8月3日
  • 有哪些激光雷达SLAM算法?

    有哪些激光雷达SLAM算法?点击上方“计算机视觉工坊”,选择“星标”干货第一时间送达来源丨https://www.zhihu.com/question/433556301编辑丨计算机视觉工坊方法一作者|牛先卓http…

    2022年6月20日
  • 开源 MQTT 服务器

    开源 MQTT 服务器到目前为止,比较流行的开源MQTT服务器有几个:1.EclipseMosquitto使用C语言实现的MQTT服务器。Eclipse组织还还包含了大量的MQTT客户端项目:https://www.eclipse.org/paho/#2.EMQX使用Erlang语言开发的MQTT服务器,内置强大的规则引擎,支持许多其他IoT协议比如MQTT-SN、CoAP、LwM2M等。3.Mosca使用Node.JS开发的MQTT服务器,简单易用

    2022年5月8日
  • SQL 函数 TIMESTAMPDIFF

    SQL 函数 TIMESTAMPDIFF①求时间差TIMESTAMPDIFF(单位,开始时间,结束时间)==开始时间-结束时间,eg。TIMESTAMPDIFF(MINUTE,begin_time,IFNULL(over_time,NOW()))注释:如果over_time不为空则用现在时间减去begin_time,得到的值的单位为分钟转载于:https://www.cnblogs…

    2022年5月7日
  • pycharm jinja2_Python django

    pycharm jinja2_Python djangojinja2语法基本语法在jinja2中,存在三种语法:控制结构{%%}变量取值{{}}注释{##}下面是一个简单的jinja2例子{#Thisisjinjacode{%forfileinfilenames%}…{%endfor%}#}可以看到,for循环的使用方式和Python比较类似,但是没有了句尾的冒号,另外需要使用endfor最为结尾,其实在jinja2…

    2022年10月23日
  • nest expression &amp; Pyparsing[通俗易懂]

    nest expression &amp; Pyparsing

    2022年1月24日

发表回复

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

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