アプリケーションでチェックボックスダイナミックを作成しようとしているときに問題が発生しています。デザインはうまく動作し、私が望むだけ多くのチェックボックスを作成することができます。チェックボックスはTextViewと一緒にTableRowに配置され、テキストはチェックボックスの左側に表示されます。しかし、私の問題は、私の活動でチェックボックスの "状態"を得ることができるかどうか、それはチェックされているかどうかです。 私はインフレータを使用して自分のチェックボックスを作成します。チェックボックスのためのxml:ダイナミックに作成されたチェックボックスのsetOnClickListeners
<TableRow xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/TableRow" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:paddingLeft="20dip" android:id="@+id/tv_effect" android:gravity="left" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:textColor="#000" android:textSize="18dip" android:layout_width="wrap_content"></TextView>
<CheckBox android:id="@+id/cb_effect" android:layout_height="wrap_content" android:text="" android:layout_gravity="right" android:layout_width="wrap_content"></CheckBox>
私はTextViewのチェックボックス含む新規のTableRow作成するために呼び出す機能:
public void layoutMakeSpeakerEffect(String effectName,int effectNumber)
{
LayoutInflater linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View myViewSp = linflater.inflate(R.layout.speaker_settings, null);
final TextView tv_effect = (TextView) myViewSp.findViewById(R.id.tv_effect);
tv_effect.setId(effectNumber);
tv_effect.setText(effectName);
final CheckBox cb_effect = (CheckBox) myViewSp.findViewById(R.id.cb_effect);
cb_effect.setId(effectNumber);
cb_effect.setText("");
cb_effect.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(cb_effect.isChecked())
{
Toast toast = Toast.makeText(getApplicationContext(), ""+tv_effect.getText()+": ON", Toast.LENGTH_SHORT);
toast.show();
}
else
{
Toast toast = Toast.makeText(getApplicationContext(), ""+tv_effect.getText()+": OFF", Toast.LENGTH_SHORT);
toast.show();
}
}
});
tl_speakerSettings.addView(myViewSp);
}
と述べたように、デザインが正常に動作します。 しかし、この機能の外でチェックボックスから「ステータス」を取得するにはどうしたらいいですか? また、チェックボックスのステータスをクリアする機能と、チェックボックスを有効または無効にする機能が必要ですか?
私はこの問題を自分で解決することはできません。
私の唯一のアイデアは、 "cb_effects" IDをチェックし、その後に希望のチェックボックスのステータスをチェックするものです。
@Cristianに感謝します。しかし、私はまだ少し混乱しています。どのように私の問題を助けることができますか?私が必要とするのは、作成したチェックボックスのステータスを保存する方法です。トグルボタンをクリックすると、チェックボックスのステータスをクリアして、トグルボタンを再度押すと「古い」ステータスを表示する必要があります。代わりにsetOnCheckedChangeListenerを実装することによって、実際にどのチェックボックスが押されているかを実際に確認できますか? – CoOkie
これは、 'buttonView'チェックボックスへの参照を提供するためです。これは、チェックボックスにしたいものを実行するために使用できるオブジェクトです: 'Checkbox bla =(チェックボックス)buttonView;'。 – Cristian
ありがとう、私はその方法を試してみる..:D – CoOkie