我正在使用此库来实现toogle按钮https://github.com/GwonHyeok/StickySwitch
如何将Switch项目作为男性或女性值转换为字符串?
MainActivity:
public class MainActivity extends AppCompatActivity {
private final String TAG = MainActivity.class.getSimpleName();
private EditText textview;
@SuppressLint("WrongViewCast")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set Selected Change Listener
StickySwitch stickySwitch = (StickySwitch) findViewById(R.id.sticky_switch);
stickySwitch.setOnSelectedChangeListener(new StickySwitch.OnSelectedChangeListener() {
@Override
public void onSelectedChange(@NotNull StickySwitch.Direction direction) {
Log.d(TAG, "Now Selected : " + direction.name());
}
});
}
}
xml文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorBackground"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:backgroundTint="#fff"
tools:context="io.ghyeok.stickyswitchdemo.MainActivity">
<io.ghyeok.stickyswitch.widget.StickySwitch
android:id="@+id/sticky_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
app:iconPadding="18dp"
app:iconSize="22dp"
app:leftIcon="@drawable/male"
app:leftText="Male"
app:rightIcon="@drawable/ic_action_name"
app:rightText="Female"
app:selectedTextSize="14sp"
app:sliderBackgroundColor="@color/colorSliderBackground"
app:switchColor="@color/colorSwitchColor"
app:textColor="@color/colorTextColor"
app:textSize="12sp" />
</RelativeLayout>
答案 0 :(得分:0)
试试这可能是有帮助的
String.valueOf(direction.name());
答案 1 :(得分:0)
如果您正在使用ToggleButton,那么您有方法:
getTextOff() which will get you the text value for when the button is off
getTextOn() which will get you the text value for when the button is on
答案 2 :(得分:0)
stickySwitch.setOnSelectedChangeListener(new StickySwitch.OnSelectedChangeListener() {
@Override
public void onSelectedChange(@NotNull StickySwitch.Direction direction) {
Log.d(TAG, "Now Selected : " + direction == StickySwitch.Direction.LEFT ?
"Male" : "Female");
}
答案 3 :(得分:0)
String gender;
if (stickySwitch.getDirection().equals(LEFT)) {
gender = "Male";
} else {
gender = "Female";
}
Log.e(TAG, "gender112: " +stickySwitch.getDirection() );
答案 4 :(得分:0)
StickySwitch库已更新至0.0.6
该版本支持以编程方式获取或设置文本。
所以如果你想获得当前状态文本
java:
stickySwitch.getText();