码迷,mamicode.com
首页 > 其他好文 > 详细

用户界面View之Switch

时间:2015-04-26 09:25:36      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:

在我们了解什么是生命之前,我们已将它消磨了一半。


本讲内容: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>

下面是MainActivity主界面文件:

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();
				}
			}
		});
	}
}



Take your time and enjoy it 


用户界面View之Switch

标签:

原文地址:http://blog.csdn.net/liguojin1230/article/details/45276107

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!