Hallo, ich bin neu hier und bräuchte mal eure Hilfe..
und zwar gehts um Radiobuttons in einemTableLayout was über eine Klasse gesteuert wird. Den Klassencode hab ich im Netz gefunden und funktionert auch soweit ganz gut.
Das einziege was ich nicht hinbekomme ist ein onClick Event auf die Buttons.. weder mit "android:onClick" per xml oder per "OnClickListener" in der Activity..
Wenn ich aber einen RadioButton gezielt ansteurere per code, funktioniert zwar das onclick event, aber dafür nicht mehr das checked event..
thx für die hilfe schon mal..
Source: http://stackoverflow.com/quest…3x3-grid-of-radio-buttons
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RadioButton;
import android.widget.TableLayout;
import android.widget.TableRow;
public class RadioButtonWithTableLayout extends TableLayout implements View.OnClickListener {
private static final String TAG = "RadioButtonWithTableLayout";
private RadioButtonWithTableLayout radGroup;
private RadioButton mBtnCurrentRadio;
public RadioButtonWithTableLayout(Context context) {
super(context);
}
public RadioButtonWithTableLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void onClick(View v) {
final RadioButton mBtnRadio = (RadioButton) v;
radGroup = (RadioButtonWithTableLayout) findViewById(R.id.rbTL);
int activeRadioButtonId = radGroup.getCheckedRadioButtonId();
// select only one radio button at any given time
if (mBtnCurrentRadio != null) {
mBtnCurrentRadio = (RadioButton) findViewById(activeRadioButtonId);
mBtnCurrentRadio.setChecked(false);
}
mBtnRadio.setChecked(true);
mBtnCurrentRadio = mBtnRadio;
}
public int getCheckedRadioButtonId() {
if ( mBtnCurrentRadio != null ) {
return mBtnCurrentRadio.getId();
}
return -1;
}
public String getCheckedRadiobuttonStringText(){
if (mBtnCurrentRadio != null){
return mBtnCurrentRadio.getText().toString();
}
return "";
}
@Override
public void addView(View child, int index,
android.view.ViewGroup.LayoutParams params) {
super.addView(child, index, params);
setChildrenOnClickListener((TableRow) child);
}
@Override
public void addView(View child, android.view.ViewGroup.LayoutParams params) {
super.addView(child, params);
setChildrenOnClickListener((TableRow) child);
}
private void setChildrenOnClickListener(TableRow tr) {
final int c = tr.getChildCount();
for (int i=0; i < c; i++) {
final View v = tr.getChildAt(i);
if (v instanceof RadioButton) {
v.setOnClickListener(this);
if (((RadioButton) v).isChecked())
mBtnCurrentRadio = (RadioButton) v;
}
}
}
}
Alles anzeigen
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private RadioButtonWithTableLayout rbTL;
private TableRow rbRow;
private RadioButton cRB;
private TextView tv_result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
HandleClick handleClick = new HandleClick();
rbTL = (RadioButtonWithTableLayout)findViewById(R.id.rbTL);
tv_result = (TextView)findViewById(R.id.txt_result);
cRB = (RadioButton)findViewById(rbTL.getCheckedRadioButtonId());
//cRB = (RadioButton)findViewById(R.id.rb_ace_pkt);
//cRB.setOnClickListener(handleClick);
// rbTL.setOnClickListener(handleClick);
}
private class HandleClick implements View.OnClickListener {
public void onClick(View v) {
int checkedId = rbTL.getCheckedRadioButtonId();
if (-1 != checkedId) {
Toast.makeText(Main3Activity.this, ((RadioButton) findViewById(checkedId)).getText() + " aktive!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(Main3Activity.this, " inaktive!!", Toast.LENGTH_SHORT).show();
}
}
}
public void onClick(View v) {
//int tag = (Integer) v.getTag();
int checkedId = rbTL.getCheckedRadioButtonId();
switch (checkedId) {
case R.id.rb_ace_pkt:
Toast.makeText(Main3Activity.this, ((RadioButton) findViewById(checkedId)).getText() + " aktive!", Toast.LENGTH_SHORT).show();
break;
case R.id.rb_ace_null:
Toast.makeText(Main3Activity.this, ((RadioButton) findViewById(checkedId)).getText() + " aktive!", Toast.LENGTH_SHORT).show();
break;
case R.id.rb_ace_fail:
Toast.makeText(Main3Activity.this, ((RadioButton) findViewById(checkedId)).getText() + " aktive!", Toast.LENGTH_SHORT).show();
break;
}
}
}
Alles anzeigen