strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">ToggleButtonDemoActivity</string>
<string name="toggle_turn_on">Turn On</string>
<string name="toggle_turn_off">Turn Off</string>
<string name="btn_display">Display</string>
</resources>


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>


java 文件


package com.cjp.togglebuttondemo;

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 ToggleButtonDemoActivity 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(ToggleButtonDemoActivity.this, result.toString(),
Toast.LENGTH_SHORT).show();

}

});

}
}



更多相关文章

  1. Java W3C 生成 XML
  2. android混淆文件
  3. android下多线程下载,断点续传,及暂停按钮
  4. cts android.hardware.camera2.cts.RecordingTest --testBasicRe
  5. Android缓存清理
  6. Android(安卓)打开资源raw文件
  7. Handler+Thread+Message模式 Android线程网络
  8. Android练习之BitmapFactory.decodeFile加载SD卡文件
  9. android 多线程下载文件案例

随机推荐

  1. 非常简单的js双向绑定框架(一)
  2. HTML Select字段通过ajax调用Web服务
  3. jQuery - 获取具有特定类的已选中复选框
  4. NodeJS - 解析JSON(只有字符串或数字)
  5. Javascript等待函数的结束,包括node.js的
  6. 如何从json对象获取匹配元素的索引?
  7. JavaScript循环输入创建一个对象数组
  8. 彻底解决IE8和IE9下ewebeditor上按钮无效
  9. 如何在JavaScript中对字符串排序
  10. 有没有办法检查两个数组是否具有相同的元