我需要将onFocusChangedListener添加到RadioGroup,以便在它失去焦点并执行验证时捕获(例如,如果用户没有选择组中的任何单选按钮)。
无线电组在某些文本字段之间。
当我点击每个字段时,我正在记录每个字段的焦点状态,而当RadioGroup失去焦点时我无法捕捉到。
使用“默认”RadioButton
时,我得到了这个<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
E/SAMPLE: input 1 focus true
E/SAMPLE: input 1 focus false
E/SAMPLE: input 2 focus true
E/SAMPLE: input 2 focus false
E/SAMPLE: input 3 focus true
E/SAMPLE: input 3 focus false
E/SAMPLE: input 4 focus true
我在RadioGroup上添加以下内容时得到了这个。
android:descendantFocusability="beforeDescendants"
android:focusable="true"
android:focusableInTouchMode="true"
E/SAMPLE: input 1 focus true
E/SAMPLE: input 1 focus false
E/SAMPLE: input 2 focus true
E/SAMPLE: input 2 focus false
E/SAMPLE: input 3 focus true
E/SAMPLE: input 3 focus false
E/SAMPLE: input 4 focus true
我在RadioGroup上添加以下内容时得到了这个。
android:descendantFocusability="afterDescendants"
android:focusable="true"
android:focusableInTouchMode="true"
E/SAMPLE: input 1 focus true
E/SAMPLE: input 1 focus false
E/SAMPLE: input 2 focus true
E/SAMPLE: input 2 focus false
E/SAMPLE: input 3 focus true
E/SAMPLE: input 3 focus false
E/SAMPLE: input 4 focus true
如您所见,我从未获得单选按钮或无线电组焦点状态。
但如果我使用tab键(在我的电脑键盘上)来改变字段,我得到这个:
E/SAMPLE: input 1 focus true
E/SAMPLE: input 1 focus false
E/SAMPLE: input 2 focus true
E/SAMPLE: input 2 focus false
E/SAMPLE: input 3 focus true
E/SAMPLE: input 3 focus false
E/SAMPLE: rb1 focus true
E/SAMPLE: rb1 focus false
E/SAMPLE: rb2 focus true
E/SAMPLE: rb2 focus false
E/SAMPLE: rb3 focus true
E/SAMPLE: rb3 focus false
E/SAMPLE: input 4 focus true
E/SAMPLE: input 4 focus false
那么,有可能关注RadioGroup吗?
提前致谢。
这是我正在使用的代码。
我的测试处方如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="beforeDescendants"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin">
<android.support.design.widget.TextInputEditText
android:id="@+id/input1_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Field 1" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin">
<android.support.design.widget.TextInputEditText
android:id="@+id/input2_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Field 2" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin">
<android.support.design.widget.TextInputEditText
android:id="@+id/input3_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Field 3" />
</android.support.design.widget.TextInputLayout>
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checkedButton="@+id/rb1"
android:descendantFocusability="beforeDescendants"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical">
<RadioButton
android:id="@+id/rb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1" />
<RadioButton
android:id="@+id/rb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2" />
<RadioButton
android:id="@+id/rb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3" />
</RadioGroup>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin">
<android.support.design.widget.TextInputEditText
android:id="@+id/input4_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Field 4" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
这是我的测试活动:
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;
import butterknife.ButterKnife;
import butterknife.OnFocusChange;
import example.com.materialsample.R;
public class FocusActivity extends AppCompatActivity {
public static Intent getStartIntent(@NonNull Context from) {
Intent intent = new Intent(from, FocusActivity.class);
return intent;
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_focus);
ButterKnife.bind(this);
}
@OnFocusChange(R.id.input1_text)
public void onInput1Focus(boolean hasFocus) {
log("input 1 focus " + hasFocus);
}
@OnFocusChange(R.id.input2_text)
public void onInput2Focus(boolean hasFocus) {
log("input 2 focus " + hasFocus);
}
@OnFocusChange(R.id.input3_text)
public void onInput3Focus(boolean hasFocus) {
log("input 3 focus " + hasFocus);
}
@OnFocusChange(R.id.input4_text)
public void onInput4Focus(boolean hasFocus) {
log("input 4 focus " + hasFocus);
}
@OnFocusChange(R.id.radioGroup) // bug in android when group is horizontal
public void onRadioGroupChanged(boolean hasFocus) {
log("group focus " + hasFocus);
}
@OnFocusChange(R.id.rb1)
public void onRadioFocus1(boolean hasFocus) {
log("rb1 focus " + hasFocus);
}
@OnFocusChange(R.id.rb2)
public void onRadioFocus2(boolean hasFocus) {
log("rb2 focus " + hasFocus);
}
@OnFocusChange(R.id.rb3)
public void onRadioFocus3(boolean hasFocus) {
log("rb3 focus " + hasFocus);
}
private void log(String msg) {
Toast.makeText(FocusActivity.this, msg, Toast.LENGTH_SHORT).show();
Log.e("SAMPLE", msg);
}
}