前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >关于Android多按钮切换的例子!

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

作者头像
全栈程序员站长
发布2022-07-20 15:29:38
发布2022-07-20 15:29:38
1K00
代码可运行
举报
运行总次数:0
代码可运行

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

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

res/values/strings.xml文件:

代码语言:javascript
代码运行次数:0
运行
复制
<?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

代码语言:javascript
代码运行次数:0
运行
复制
<?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

代码语言:javascript
代码运行次数:0
运行
复制
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

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年3月1,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档