Android: How to create slide (on/off) button

后端 未结 3 1886
隐瞒了意图╮
隐瞒了意图╮ 2020-12-08 23:03

I\'d like to create a slide button (= something as switch ) with two states: on and off so user will have to press the button and slide it to change the state (something sim

3条回答
  •  没有蜡笔的小新
    2020-12-08 23:55

    //in your layout design the below line

    
    
    
       
    

    // in your activity call this

    ImageView mNotification_on_btn=(ImageView)findViewById(R.id.on_btn);
    ImageView mNotification_off_btn=(ImageView)findViewById(R.id.off_btn);
    
        mNotification_on_btn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    mNotification_on_btn.setVisibility(View.GONE);
                    mNotification_off_btn.setVisibility(View.VISIBLE);
                }
            });
        mNotification_off_btn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    mNotification_off_btn.setVisibility(View.GONE);
                    mNotification_on_btn.setVisibility(View.VISIBLE);
                }
            });
    

    // this will switch like iphone style on off toggle buttonenter image description here enter image description here

提交回复
热议问题