原創作品,允許轉載,轉載時請務必以超鏈接形式標明文章 原始出處 、作者信息和本聲明。否則將追究法律責任。http://liangruijun.blog.51cto.com/3061169/655014
ToggleButton(開關按鈕)是Android系統中比較簡單的一個組件,是一個具有選中和未選擇狀態雙狀態的按鈕,並且需要為不同的狀態設置不同的顯示文本。
ToggleButton常用的XML屬性

屬性名稱
描述
android:disabledAlpha
設置按鈕在禁用時透明度。

android:textOff
未選中時按鈕的文本
android:textOn
選中時按鈕的文本

下面是具體的例子:
第一個例子是通過Toast顯示ToggleButton不同的狀態時的信息
MainActivity.java
package com.android.togglebutton;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity extends Activity {
//聲明ToggleButton
private ToggleButton togglebutton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

togglebutton = (ToggleButton) findViewById(R.id.togglebutton);
togglebutton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// 當按鈕第一次被點擊時候響應的事件
if (togglebutton.isChecked()) {
Toast.makeText(MainActivity.this, "你喜歡球類運動", Toast.LENGTH_SHORT).show();
}
// 當按鈕再次被點擊時候響應的事件
else {
Toast.makeText(MainActivity.this, "你不喜歡球類運動", Toast.LENGTH_SHORT).show();
}
}
});
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<ToggleButton
android:id="@+id/togglebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="喜歡"
android:textOff="不喜歡"
/>
</LinearLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">你喜不喜歡球類運動?</string>
<string name="app_name">測試ToggleButton</string>
</resources>
效果圖:

第二個例子通過圖片的變化顯示ToggleButton不同的狀態時的圖片
MainActivity.java
package com.android.togglebutton;

import android.app.Activity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.ToggleButton;

public class MainActivity extends Activity {
//聲明ImageView,ToggleButton
private ImageView imageView;
private ToggleButton toggleButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//通過findViewById獲得ImageView,ToggleButton
imageView=(ImageView) findViewById(R.id.imageView);
toggleButton=(ToggleButton)findViewById(R.id.toggleButton);

toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
toggleButton.setChecked(isChecked);
//使用三目運算符來響應按鈕變換的事件
imageView.setImageResource(isChecked?R.drawable.pic_on:R.drawable.pic_off);
}
});
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/pic_off"
android:layout_gravity="center_horizontal"
/>
<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="130dip"
android:layout_height="wrap_content"
android:textOn="開燈"
android:textOff="關燈"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>
效果圖:

本文出自 「IT的點點滴滴」 博客,請務必保留此出處http://liangruijun.blog.51cto.com/3061169/655014

arrow
arrow
    全站熱搜

    戮克 發表在 痞客邦 留言(0) 人氣()