我在viewpager中有多个片段的应用程序。这些片段都显示EditText,其中大多数都具有inputtype numberDecimal,其中三个没有指定inputtype。 在此viewpager之外,还有另一个edittext用作搜索框。
我在EditText的触摸事件中做了一些手动聚焦,以在viewpager中获得正确的EditText焦点。 (因为类似于此处描述的问题:ViewPager focus issue)
当我在ViewPager中选择第一个EditText时,右键盘类型显示(nummeric)。现在,当我选择下一个EditText时,默认键盘会在指定inpytType numberDecimal时显示。 当我在viewPager之外删除(搜索)EditText并重复上述场景时,第二个EditText会获得正确的键盘类型。 对我来说,几乎看起来搜索EditText正在以某种方式从我在片段中的EditText中窃取焦点,这些内容位于viewpager中。
如何阻止它这样做?
我已经给了它的父母android:focusableInTouchMode =" true"属性。
我的主要活动
[Activity(Label = "viewpagertest", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : FragmentActivity
{
private CustomViewPager _viewPager;
private CustomViewPagerAdapter _adapter;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
_viewPager = FindViewById<CustomViewPager>(Resource.Id.viewPager);
_adapter = new CustomViewPagerAdapter(SupportFragmentManager);
_viewPager.Adapter = _adapter;
_viewPager.PageSelected += _viewPager_PageSelected;
}
void _viewPager_PageSelected(object sender, Android.Support.V4.View.ViewPager.PageSelectedEventArgs e)
{
_viewPager.UnfocusFocusedView();
}
public void EditTextFieldClicked(View parentView, EditText editText)
{
_viewPager.RequestManualChildFocus(parentView, editText);
}
private class CustomViewPagerAdapter : FragmentPagerAdapter
{
private readonly List<Fragment> _fragments;
private int FragmentsPerPage { get { return 5; } }
public CustomViewPagerAdapter(FragmentManager fm)
: base(fm)
{
_fragments = new List<Fragment>();
for (int i = 0; i < 20; i++)
{
if (i == 3 || i == 9 || i == 15)
{
_fragments.Add(new FragmentTestString());
}
else
{
_fragments.Add(new FragmentTest());
}
}
}
public override Fragment GetItem(int position)
{
return _fragments[position];
}
public override int GetItemPosition(Java.Lang.Object @object)
{
return PositionNone;
}
public override int Count
{
get { return 20; }
}
public override float GetPageWidth(int position)
{
return 1.0f / FragmentsPerPage;
}
}
}
我的MainActivity xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:focusableInTouchMode="true"
android:layout_height="fill_parent">
<EditText
android:id="@+id/etxtSearch"
android:hint="Search"
android:imeOptions="actionDone"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<viewpagertest.CustomViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
我的自定义viewpager可以手动对焦
class CustomViewPager : ViewPager
{
private View _prevFocusedView;
protected CustomViewPager(IntPtr javaReference, JniHandleOwnership transfer)
: base(javaReference, transfer)
{
}
public CustomViewPager(Context context)
: base(context)
{
}
public CustomViewPager(Context context, IAttributeSet attrs)
: base(context, attrs)
{
}
public override void RequestChildFocus(View child, View focused)
{
//Do nothing, disables automatic focus behaviour
}
public void RequestManualChildFocus(View child, View focused)
{
if (_prevFocusedView != focused)
{
if (_prevFocusedView != null)
{
_prevFocusedView.ClearFocus();
}
base.RequestChildFocus(child, focused);
_prevFocusedView = focused;
}
}
public void UnfocusFocusedView()
{
var etxt = _prevFocusedView as EditText;
if (etxt != null)
{
var imm =
(InputMethodManager)Context.GetSystemService(Android.Content.Context.InputMethodService);
imm.HideSoftInputFromWindow(etxt.WindowToken, 0);
}
if (_prevFocusedView != null)
{
_prevFocusedView.ClearFocus();
}
_prevFocusedView = null;
}
}
我的片段
public class FragmentTest : Fragment
{
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var view = inflater.Inflate(Resource.Layout.fragment, container, false);
var editText = view.FindViewById<EditText>(Resource.Id.etxtInput);
editText.Touch += editText_Touch;
return view;
}
void editText_Touch(object sender, View.TouchEventArgs e)
{
var etxtView = sender as EditText;
if (etxtView != null)
{
var activity = Activity as MainActivity;
if (activity != null)
{
activity.EditTextFieldClicked(this.View, sender as EditText);
}
etxtView.OnTouchEvent(e.Event);
}
}
}
片段xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:focusableInTouchMode="true"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:id="@+id/etxtInput"
android:inputType="numberDecimal|numberSigned"
android:imeOptions="actionDone|flagNoExtractUi"
android:layout_width="match_parent"
android:layout_height="wrap_content" />