如何触发单选按钮以检查是否按了线性布局 [英] How to trigger radioButton to checked if I pressed on Linear layout
问题描述
如果我在1线性布局内的其他位置(而不是单选按钮)上但在图像,文本视图等上按下,如何使单选按钮被选中.
How can I make the radio button get selected if I pressed on other place ( not the radio button ) but on image, text view and etc inside 1 Linear layout.
<?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"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="@+id/layout_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/card_balance"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="center_vertical"
android:paddingLeft="20dp" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingLeft="10dp"
android:textColor="@color/dc_white" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="right"
android:paddingRight="20dp">
<RadioButton
android:id="@+id/radio_button_payment"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
</LinearLayout>
</RelativeLayout>
这是adapater:PaymentAdapter.java
And here is the adapater: PaymentAdapter.java
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View allView = inflater.inflate(R.layout.item_choose_payment, parent, false);
TextView txt1 = (TextView) allView.findViewById(R.id.textView1);
ImageView imageView = (ImageView) allView.findViewById(R.id.card_balance);
final RadioButton r = (RadioButton) allView.findViewById(R.id.radio_button_payment);
LinearLayout layout_card = (LinearLayout) allView.findViewById(R.id.layout_1);
r.setChecked(position == selectedPosition);
r.setTag(position);
r.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
selectedPosition = (Integer) view.getTag();
notifyDataSetChanged();
}
});
}
现在的条件是我只能在单选按钮上按下.
Right now the condition is I can pressed only at radio button.
我已经尝试过将onClickListener添加到线性布局,是的,我可以按线性布局,但是单选按钮不仅选择了1,还选择了全部.
I've already tried to add onClickListener to linear layout, and yes I can pressed on the linear layout however the Radio button not select only 1 but all selected.
此代码用于linearLayout onClick
this code for linearLayout onClick
layout_card.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
r.setChecked(position == selectedPosition);
r.setTag(position);
if (r.isChecked()) {
r.setChecked(true);
r.setSelected(true);
} else if (!r.isChecked()) {
r.setChecked(false);
r.setSelected(false);
}
}
});
对不起,我的解释很不好,对于我的帮助,我深表感谢. 谢谢
Sorry for my bad explanation, any help is very grateful for me. Thank you
推荐答案
在您的 getView(...)内,写
layout_card.setTag(position);
并将点击侦听器更改为
layout_card.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectedPosition = Integer.parseInt(v.getTag().toString());
notifyDataSetChange();
}
});
如果不需要,请删除下面的代码
remove below code, if not needed
r.setTag(position);
r.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
selectedPosition = (Integer) view.getTag();
notifyDataSetChanged();
}
});
这篇关于如何触发单选按钮以检查是否按了线性布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!