android mvp示例_Android开关按钮示例

android mvp示例

Here you will get android switch button example.

在这里,您将获得android switch button的示例

Switch is a button which has two states, on or off. The user can on or off by just dragging or tapping finger on the button. This UI widget is supported on android version 4.0 and higher.

开关是一个具有打开或关闭两种状态的按钮。 用户只需在按钮上拖动或点击手指即可打开或关闭。 Android 4.0及更高版本支持此UI小部件。

We can define switch button in XML layout by using <Switch> tag.

我们可以使用<Switch>标签在XML布局中定义切换按钮。

The OnCheckedChangeListener is applied on button to get notified whenever its state is changed.

OnCheckedChangeListener应用于按钮,以便在状态改变时得到通知。

The isChecked() method is used to check the current state of switch. It returns true if state is ON else returns false.

isChecked()方法用于检查开关的当前状态。 如果状态为ON,则返回true,否则返回false。

You can set the initial state of switch by setChecked() method in following way.

您可以通过以下方法通过setChecked()方法设置开关的初始状态。

//set switch button to ON
sButton.setChecked(true);
 
//set switch button to OFF
sButton.setChecked(false);

Android开关按钮示例 (Android Switch Button Example)

activity_main.xml

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center">
 
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:id="@+id/text1"
        android:layout_marginBottom="10dp"
        android:text="Play With Me..."/>
 
    <Switch
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/sButton"/>
 
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:id="@+id/text2"
        android:layout_marginTop="10dp"/>
</LinearLayout>

MainActivity.java

MainActivity.java

package thecrazyprogrammer.androidexample;
 
import android.app.Activity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
 
public class MainActivity extends Activity {
    Switch sButton;
    TextView text2;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        sButton = (Switch)findViewById(R.id.sButton);
        text2 = (TextView)findViewById(R.id.text2);
 
        if(sButton.isChecked()){
            text2.setText("Switch Button is On");
        }
        else{
            text2.setText("Switch Button is Off");
        }
 
        sButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked == true){
                    Toast.makeText(MainActivity.this,"On",Toast.LENGTH_SHORT).show();
                    text2.setText("Switch Button is On");
                }
                else{
                    Toast.makeText(MainActivity.this,"Off",Toast.LENGTH_SHORT).show();
                    text2.setText("Switch Button is Off");
                }
            }
        });
    }
}

Screenshot

屏幕截图

Android Switch Button Example

Comment below if you have any queries related to above android switch button example.

如果您对上述android开关按钮示例有任何疑问,请在下面评论。

翻译自: https://www.thecrazyprogrammer.com/2016/08/android-switch-button-example.html

android mvp示例

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值