gpt4 book ai didi

android - 在 View 分页器中的页面之间移动时,Edittext 自动获得焦点

转载 作者:太空宇宙 更新时间:2023-11-03 11:03:00 24 4
gpt4 key购买 nike

我有一个 Activity 在 View 寻呼机中托管两个 fragment 。我使用相同的布局来膨胀这些 fragment 。该布局有两个编辑文本放置在线性布局内,线性布局位于相对布局内。问题是当我从 fragment A 切换到 fragment B 时,他首先编辑的文本在 fragment A 中具有焦点,当我从 fragment B 返回到 fragment A 时,而不是第一个编辑文本具有焦点,第二个编辑文本获得焦点.如何解决。我在下面提供布局和源代码。我没有在 fragment 类中返回任何代码。

Activity :

public class LoginActivity extends BaseActivity {
public static final String selectedTabPosition = "selectedTabPosition";
//Tab tag name
private static String TAB_1_TAG = "Email";
private static String TAB_2_TAG = "Mobile";
private TabLayout mTabLayout;
private ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

TAB_1_TAG = getResources().getString(R.string.tab_email);
TAB_2_TAG = getResources().getString(R.string.tab_mobile);

//Initialise views
mViewPager = (ViewPager) findViewById(R.id.viewpager);
mTabLayout = (TabLayout) findViewById(R.id.tabs);

//set tab with view pager
setupViewPager(mViewPager);
mTabLayout.setupWithViewPager(mViewPager);
setupTabIcons();

mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

}

@Override
public void onPageSelected(int position) {
dismissKeyboard(mViewPager);
}

@Override
public void onPageScrollStateChanged(int state) {

}
});
}

/**
* Adding custom view to tab
*/
private void setupTabIcons() {
LinearLayout tabOne = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.tab_custom, null);
TextView tvIconOne = (TextView) tabOne.findViewById(R.id.tv_tab_title);
tvIconOne.setText(TAB_1_TAG);
mTabLayout.getTabAt(0).setCustomView(tabOne);
setTypeface(tvIconOne, CustomFonts.Prime_regular);
mTabLayout.getTabAt(0).getCustomView().setSelected(true);

LinearLayout tabTwo = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.tab_custom, null);
TextView tvIconTwo = (TextView) tabTwo.findViewById(R.id.tv_tab_title);
tvIconTwo.setText(TAB_2_TAG);
setTypeface(tvIconTwo, CustomFonts.Prime_regular);
mTabLayout.getTabAt(1).setCustomView(tabTwo);
}

/**
* Adding fragments to ViewPager
*
* @param viewPager The view pager
*/
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
LoginFragment loginFragmentEmail = new LoginFragment();
Bundle emailBundle = new Bundle();
emailBundle.putInt(selectedTabPosition, 0);
loginFragmentEmail.setArguments(emailBundle);
LoginFragment loginFragmentMobile = new LoginFragment();
Bundle phoneBundle = new Bundle();
phoneBundle.putInt(selectedTabPosition, 1);
loginFragmentMobile.setArguments(phoneBundle);
adapter.addFrag(loginFragmentEmail, TAB_1_TAG);
adapter.addFrag(loginFragmentMobile, TAB_2_TAG);
viewPager.setAdapter(adapter);
}

class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();

public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}

@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}

@Override
public int getCount() {
return mFragmentList.size();
}

public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}

@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}

fragment :

    public class LoginFragment extends BaseFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View rootView = inflater.inflate(R.layout.fragment_login, container, false);
return rootView;

}

activity_login.xml:

 <?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="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical">

<ImageView
android:id="@+id/iv_title"
android:layout_width="200dp"
android:layout_height="45dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
/>

<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="@dimen/custom_tab_layout_height"
android:layout_below="@+id/iv_title"
android:layout_marginTop="@dimen/spacing_10"
app:tabGravity="fill"
app:tabIndicatorColor="@color/app_color_dark"
app:tabMode="fixed"/>

<View
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="@dimen/divider_height_small"
android:layout_below="@+id/tabs"
android:background="@color/gray_medium"/>

<com.helper.CustomNonSwipeableViewpager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/view"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

</RelativeLayout>

fragment_login.xml:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/spacing_50"
android:orientation="vertical">

<EditText
android:id="@+id/et_email"
android:layout_width="match_parent"
android:layout_height="@dimen/spacing_48"
/>

<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="@dimen/spacing_48"
android:layout_marginTop="@dimen/spacing_15"/>

</LinearLayout>

</RelativeLayout>

AndroidManifest.xml:

 <activity
android:name=".activities.LoginActivity"
android:screenOrientation="portrait"
/>

在AndrodManifest文件中,尝试了

android:windowSoftInputMode = stateHidden and android:windowSoftInputMode = adjustPan

CustomNonSwipeableViewpager.java:

    public class CustomNonSwipeableViewpager extends ViewPager {

public CustomNonSwipeableViewpager(Context context) {
super(context);
}

public CustomNonSwipeableViewpager(Context context, AttributeSet attrs) {
super(context, attrs);
}

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
// Never allow swiping to switch between pages
return false;
}

@Override
public boolean onTouchEvent(MotionEvent event) {
// Never allow swiping to switch between pages
return false;
}
}

屏幕截图:在移动到 FragmentB 之前:

Before moving to FragmentB.

在 fragment B 中: In Fragment B.

从 fragment B 返回 fragment A:

enter image description here

最佳答案

您可以在 fragment 运行期间禁用对 EditText 的关注:

EditText et_email_view = (EditText) rootView.findViewById(R.id.et_email);
et_email_view.setFocusable(false);

关于android - 在 View 分页器中的页面之间移动时,Edittext 自动获得焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38177244/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com