Подскажите, что это за кнопка? Как она называется? И как такую сделать?
1 ответ
Это switch button.
По ссылке:
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:layout_margin="16dp"
android:gravity="center_horizontal"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android Switch Button Example" />
<Switch
android:id="@+id/switchButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="Android Switch Button 1" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android Switch Button" />
<Switch
android:id="@+id/switchButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="Android Switch Button 2" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android Switch Button" />
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center|bottom"
android:text="ViralAndroid.com"
android:textSize="24sp"
android:textStyle="bold" />
</LinearLayout>
Activity:
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Switch switchButton, switchButton2;
TextView textView, textView2;
String switchOn = "Switch is ON";
String switchOff = "Switch is OFF";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.switch_button_example);
// For first switch button
switchButton = (Switch) findViewById(R.id.switchButton);
textView = (TextView) findViewById(R.id.textView);
switchButton.setChecked(true);
switchButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean bChecked) {
if (bChecked) {
textView.setText(switchOn);
} else {
textView.setText(switchOff);
}
}
});
if (switchButton.isChecked()) {
textView.setText(switchOn);
} else {
textView.setText(switchOff);
}
// for second switch button
switchButton2 = (Switch) findViewById(R.id.switchButton2);
textView2 = (TextView) findViewById(R.id.textView2);
switchButton2.setChecked(false);
switchButton2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean bChecked) {
if (bChecked) {
textView2.setText(switchOn);
} else {
textView2.setText(switchOff);
}
}
});
if (switchButton2.isChecked()) {
textView2.setText(switchOn);
} else {
textView2.setText(switchOff);
}
}
}
Как будет выглядеть: