标签:
在我们了解什么是生命之前,我们已将它消磨了一半。
本讲内容:Switch 开关控件
示例一:
下面是res/layout/activity_main.xml 布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="WIFI设置"
android:textSize="20sp" />
<Switch
android:id="@+id/open"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:textOn="WIFI开启中"
android:textOff="WIFI关闭中"/>
</RelativeLayout>public class MainActivity extends Activity {
private Switch open;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
open=(Switch) findViewById(R.id.open);
open.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
Toast.makeText(MainActivity.this, "打开WIFI", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(MainActivity.this, "关闭WIFI", Toast.LENGTH_LONG).show();
}
}
});
}
}
标签:
原文地址:http://blog.csdn.net/liguojin1230/article/details/45276107