ToggleButton作用:用于控制某个View的状态,参数,内容等,使得对应的值循环转换。
使用第一步:
1,在XML之中定于它,并且写上它想控制的视图。
第二部:在Activity里设置ToggleButton的监听器。
使用第一步:
1,在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="wrap_content"
android:orientation="vertical" >
<ToggleButton
android:id="@+id/toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:textOff="横向排列横向排列"
android:textOn="纵向排列横向排列" />
<LinearLayout
android:id="@+id/test"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/btone"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮一" />
<Button
android:id="@+id/bttwo"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮二" />
<Button
android:id="@+id/btthree"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮三" />
</LinearLayout>
</LinearLayout>
第二部:在Activity里设置ToggleButton的监听器。
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ToggleButton;
public class ToggleButtonActivity extends Activity {
private Button button = null;
private EditText editText = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.togglebutton);
// Listen for button clicks
ToggleButton toggle=(ToggleButton)findViewById(R.id.toggle);
final LinearLayout test=(LinearLayout)findViewById(R.id.test);
toggle.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
if(arg1){
//设置LinearLayout垂直布局
test.setOrientation(LinearLayout.VERTICAL);
}
else{
//设置LinearLayout水平布局
test.setOrientation(LinearLayout.HORIZONTAL);
}
}
});
}
}