横向开关(switch)

一:属性

横向开关(switch)

1.Activity

//横向开关
public class SwitchActivity extends Activity {
    
    private Switch switchComponent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.switch_layout);
        
        switchComponent = (Switch)findViewById(R.id.switchId);
        
        switchComponent.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    Toast.makeText(SwitchActivity.this, "无线网络打开了!", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(SwitchActivity.this, "无线网络关闭了!", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}

2.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<!-- 横向开关 -->
<LinearLayout xmlns:andro>
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:padding="5dp" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="无线网:"
        android:textSize="20sp" />

    <Switch
        android:id="@+id/switchId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:textOn="打开"
        android:textOff="关闭"
        />

</LinearLayout>

3.效果图如下:

横向开关(switch)

相关文章:

  • 2022-12-23
  • 2021-12-27
  • 2021-10-18
  • 2021-12-25
  • 2021-09-07
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-05-15
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案