2016-10-05 20 views
2

私は単純なチェックボックスを作ろうとしています。チェックボックスをオンにすると、チェックボックスのテキストでトーストを表示します。チェックボックスのループとAndroidのテキストの取得

これは私が試してみましたが、

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

関連する問題