關於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>
image.gif
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>
image.gif
筆記
Review the “togglebutton2”, we did customized the togglebutton2’s display text on and off and made it checked by default.
有興趣的加入Android工程師交流Q群:752016839 主要針對Android開發人員提升自己,突破瓶頸,相信你來學習,會有提升和收穫。
三.程式碼程式碼
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(); } }); } }
image.gif
-
Demo
Run the application.
-
Result, toggleButton2 is using the customized string, and checked by default.

圖片描述
image.gif
android togglebutton demo1
- Checked toggleButton1 and unchecked toggleButton2, and click on the display button, the current state of both toggle buttons will be displayed.

圖片描述
image.gif
android togglebutton demo2