本文主要是介绍Android ToggleButton 开关按钮,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
ToggleButton相关属性,方法android:textOn="On" android:textOff="Off" android:checked="true"setChecked(boolean)
package shortcut.song.com.myapplication;import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.CompoundButton; import android.widget.Toast; import android.widget.ToggleButton;public class ToggleButtonActivity extends AppCompatActivity {ToggleButton mToggleButton1;ToggleButton mToggleButton2;@Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_toggle_button);mToggleButton1 = (ToggleButton)findViewById(R.id.togglebutton_1);mToggleButton2 = (ToggleButton)findViewById(R.id.togglebutton_2);mToggleButton1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {if ( isChecked ){Toast.makeText(ToggleButtonActivity.this, "ON", Toast.LENGTH_SHORT).show();} else{Toast.makeText(ToggleButtonActivity.this, "OFF", Toast.LENGTH_SHORT).show();}}});mToggleButton2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {if ( isChecked ){Toast.makeText(ToggleButtonActivity.this, "大", Toast.LENGTH_SHORT).show();}else{Toast.makeText(ToggleButtonActivity.this, "小", Toast.LENGTH_SHORT).show();}}});}}
layout布局文件内容
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_toggle_button" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="shortcut.song.com.myapplication.ToggleButtonActivity"><LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content"><ToggleButton android:id="@+id/togglebutton_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOn="On" android:textOff="Off" /><ToggleButton android:id="@+id/togglebutton_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOn="大" android:textOff="小" /></LinearLayout> </RelativeLayout>
这篇关于Android ToggleButton 开关按钮的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!