2016-10-05 3 views
2

간단한 체크 박스를 만들려고하는데, 체크 박스가 선택되면 체크 박스 텍스트로 토스트를 표시하고 싶습니다. 체크 박스를 반복하면서 안드로이드에서 텍스트 받기

내 내가 시도 코드 만이 내 레이아웃입니다

nextscreen.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       RelativeLayout layout = (RelativeLayout)findViewById(R.id.DSP); 
       for (int i = 0; i < layout.getChildCount(); i++) { 
        v = layout.getChildAt(i); 
        if (v instanceof CheckBox) { 
         if (((CheckBox) v).isChecked()){ 
          String text = String.valueOf(((CheckBox) v).getText()); 

          Log.d("@@@@@@@@@" , "somethinng" +text); 

        } 

        } 

       } 
      } 
     }); 

을 작동하지 않습니다.

<RelativeLayout 
    android:layout_below="@+id/customer_email_addnew_edittext" 
    android:layout_width="match_parent" 
    android:id="@+id/DSP" 
    android:layout_height="match_parent"> 

    <android.support.v7.widget.AppCompatCheckBox 
     android:id="@+id/toi" 
     android:layout_width="match_parent" 
     android:layout_margin="5dp" 
     android:layout_height="wrap_content" 
     android:text="Times of India" 
     android:background="@drawable/customborder"/> 

    <android.support.v7.widget.AppCompatCheckBox 
     android:id="@+id/danikjagran" 
     android:layout_width="match_parent" 
     android:layout_below="@+id/toi" 
     android:layout_margin="5dp" 
     android:layout_height="wrap_content" 
     android:text="Times of India" 
     android:background="@drawable/customborder"/> 

    <android.support.v7.widget.AppCompatCheckBox 
     android:id="@+id/htimes" 
     android:layout_width="match_parent" 
     android:layout_below="@+id/danikjagran" 
     android:layout_margin="5dp" 
     android:layout_height="wrap_content" 
     android:text="Times of India" 
     android:background="@drawable/customborder"/> 

    <Button 
     android:id="@+id/conntinue" 
     android:text="continue" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true"/> 
</RelativeLayout> 

모든 확인란을 반복하여 텍스트를 가져올 수있는 방법이 있습니까?

답변

1

제시 한 코드에 Toast이 표시되지 않고 대신 로그에 기록됩니다.

if (((CheckBox) v).isChecked()) { 
    String text = String.valueOf(((CheckBox) v).getText()); 
    Toast.makeText(this, text, Toast.LENGTH_SHORT).show(); 
} 

UPDATE 당신이 파단 매번 반복 할 필요가 없습니다이 방법 :

이 시도는 일

public class YourActivity extends Activity { 

    private android.support.v7.widget.AppCompatCheckBox mToi; 
    private android.support.v7.widget.AppCompatCheckBox mDanikjagran; 
    private android.support.v7.widget.AppCompatCheckBox mHtimes; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.your_activity); 

     mToi = (android.support.v7.widget.AppCompatCheckBox) findViewById(R.id.toi); 
     mDanikjagran = (android.support.v7.widget.AppCompatCheckBox) findViewById(R.id.danikjagran); 
     mHtimes = (android.support.v7.widget.AppCompatCheckBox) findViewById(R.id.htimes); 

     // ... 

     nextscreen.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if (mToi.isChecked()) { 
        Toast.makeText(this, mToi.getText(), Toast.LENGTH_SHORT).show(); 
       } 
       if (mDanikjagran.isChecked()) { 
        Toast.makeText(this, mDanikjagran.getText(), Toast.LENGTH_SHORT).show(); 
       } 
       if (mHtimes.isChecked()) { 
        Toast.makeText(this, mHtimes.getText(), Toast.LENGTH_SHORT).show(); 
       } 
      } 
     }); 
    } 
} 
+0

감사합니다,하지만 당신은 다른이 할 동일한 비트를보다 효율적으로 수행하는 논리. – Firdoesh

+0

@Firdoesh가 예제를 추가했습니다. – nandsito

관련 문제