当前位置:首页 > 安卓源码 > 技术博客 >

关于Android的多按钮切换的例子实践

时间:2018-01-08 17:42 来源:互联网 作者:源码搜藏 浏览: 收藏 挑错 推荐 打印

1.自定义字符串 打开res / values / strings.xml文件,为切换按钮添加一些自定义字符串。 RES /值/ strings.xml中文件: ? xml version= 1.0 encoding= utf-8 ? resources string name = app_name MyAndroidApp / string string name = toggle_turn_on Turn

1.自定义字符串
打开“res / values / strings.xml”文件,为切换按钮添加一些自定义字符串。

RES /值/ 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.切换按钮
打开“res / layout / main.xml”文件,在线性布局内添加两个“切换按钮”和一个普通的按钮。

文件: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>

笔记
回顾“togglebutton2”,我们确实定制了togglebutton2的显示文字,并且默认情况下已经勾选。

三。代码代码
在内部活动“onCreate()”方法中,在正常按钮上附加一个点击监听器,显示切换按钮的当前状态。

文件: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. 演示
    运行应用程序。

  2. 结果,toggleButton2正在使用自定义字符串,并在默认情况下进行检查。

关于Android的多按钮切换的例子实践
android togglebutton demo1

  1. 选中toggleButton1并取消选中toggleButton2,然后单击显示按钮,将显示两个切换按钮的当前状态。

关于Android的多按钮切换的例子实践
android togglebutton demo2

关于Android的多按钮切换的例子实践 转载https://www.codesocang.com/appboke/38287.html

技术博客阅读排行

最新文章