2017-03-20 9 views
-1

이 라이브러리를 사용하여 예지 버튼을 사용합니다. https://github.com/GwonHyeok/StickySwitch토글을 선택하거나 선택한 항목 값을 문자열로 전환 하시겠습니까?

전환 항목을 남성 또는 여성 값으로 문자열로 가져올 수있는 방법은 무엇입니까?

enter image description here

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

'direction.name을()'당신이 원하는 것입니다. 로그보기 :'Log.d (TAG, "Now Selected :"+ direction.name()); ' –

+0

이것은 왼쪽과 오른쪽으로 나에게 줄 것입니다. 남성 또는 여성을 문자열 –

+0

에 저장하고'String value; if (direction.name(). equals ("LEFT")) {value = "Male"; } else {value = "Female";}' –

답변

0

것이 도움이

String.valueOf(direction.name()); 
있어이있을 수 있습니다 시도
+0

이것은 왼쪽 & 오른쪽 줄 것이다. 나는 남성 또는 여성을 끈으로 묶고 싶다. –

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 
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"); 
    } 
+0

오류 : (34, 74) 오류 : 기호 변수를 찾을 수 없습니다. 방향 –

+0

@User_FIt'StickySwitch.Direction' –

+0

감사합니다 – Charlie

0
     String gender; 
         if (stickySwitch.getDirection().equals(LEFT)) { 
           gender = "Male"; 
         } else { 
           gender = "Female"; 
         } 
         Log.e(TAG, "gender112: " +stickySwitch.getDirection()); 
0

StickySwitch 라이브러리 버전이 지원을받을 또는 프로그래밍 텍스트를 설정입니다 0.0.6

으로 업데이트되었습니다.

그래서 당신이 현재 상태 텍스트를

자바를 얻으려면 :

stickySwitch.getText(); 

상세 정보 : Sticky Switch - Set, Get Text Programmatically