用户界面View之Switch与ToggleButton

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


本讲内容:Switch 开关控件和ToggleButton控件


示例一:


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


示例二:

  

下面是res/layout/activity_main.xml 布局文件:

<LinearLayout 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"
    android:orientation="vertical" >

    <!-- textOn:true   textOff:false  默认是false-->
    <ToggleButton
        android:id="@+id/id_toggleButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textOff="关"
        android:textOn="开" 
        android:checked="false"/>
    
    <ImageView 
        android:id="@+id/id_imageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/off"/>

</LinearLayout>


下面是MainActivity主界面文件:

public class MainActivity extends Activity implements OnCheckedChangeListener{
	private ToggleButton tb;
	private ImageView img;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		
		tb=(ToggleButton) findViewById(R.id.id_toggleButton);
		img=(ImageView) findViewById(R.id.id_imageView);
		tb.setOnCheckedChangeListener(this);
		
	}

	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		/*
		 *当tb被点击的时候,当前的方法会执行
		 * buttonView  代表被点击控件的本身
		 * isChecked   代表被点击的控件的状态
		 */
		img.setBackgroundResource(isChecked?R.drawable.on:R.drawable.off);
	}
}


Take your time and enjoy it 




你可能感兴趣的:(用户界面View之Switch与ToggleButton)