大家好,我在onFocusChangeListener上遇到了一些问题。 这是侦听器的代码:
EditText birthday = (EditText)view.findViewById(R.id.signup_birthday_input);
birthday.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus) SignUpActivity.showDatePicker();
}
});
这是在PagerAdapter内部执行的,我的编辑文本在页面内部。
如果将showDatePicker设置为布局的onClick参数,则可以使用。
我试图对焦点改变进行打印,但是什么也没发生,就像它永远都无法获得焦点(但是我敢肯定,因为它显示了键盘)。
这是整个PagerAdapter类
class SignUpProcessPager extends PagerAdapter {
private Context context;
public SignUpProcessPager(Context context) {
this.context = context;
}
@Override
public Object instantiateItem(ViewGroup collection, int position) {
ModelObject modelObject = ModelObject.values()[position];
LayoutInflater inflater = LayoutInflater.from(context);
ViewGroup layout = (ViewGroup) inflater.inflate(modelObject.getLayoutResId(), collection, false);
collection.addView(layout);
View view = inflater.inflate(R.layout.sign_up_info, null);
EditText birthday = (EditText)view.findViewById(R.id.signup_birthday_input);
birthday.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus) SignUpActivity.showDatePicker();
}
});
return layout;
}
@Override
public void destroyItem(ViewGroup collection, int position, Object view) {
collection.removeView((View) view);
}
@Override
public int getCount() {
return ModelObject.values().length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public CharSequence getPageTitle(int position) {
ModelObject customPagerEnum = ModelObject.values()[position];
return context.getString(customPagerEnum.getTitleResId());
}
}
ShowDatePicker方法应显示日期选择器并隐藏键盘。
希望有人可以帮助☺️!!