ToggleButton的使用

来源:互联网 发布:淘宝上的atinis可靠吗 编辑:程序博客网 时间:2024/05/29 15:55

xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <ToggleButton        android:id="@+id/tb"        android:layout_width="wrap_content"        android:layout_height="wrap_content"         android:textOn="打开"        android:textOff="关闭"/>    <TextView        android:id="@+id/show"        android:layout_width="match_parent"        android:layout_height="wrap_content" /></LinearLayout>

java

package com.example.assembly;import android.app.Activity;import android.os.Bundle;import android.widget.CompoundButton;import android.widget.CompoundButton.OnCheckedChangeListener;import android.widget.TextView;import android.widget.ToggleButton;public class MainActivity extends Activity {private TextView showTv;private ToggleButton tb;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);showTv=(TextView) findViewById(R.id.show);tb=(ToggleButton) findViewById(R.id.tb);tb.setOnCheckedChangeListener(new OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {if (isChecked) {showTv.setText("打开");}else {showTv.setText("关闭");}}});}}

效果图:

          



0 0
原创粉丝点击