如何以编程方式取消选择TextInputLayout边框

问题描述 投票:1回答:4

我想在用户按下后退按钮或取消选项菜单编辑项时取消选择TextInputLayout。因此,正如您在图片中看到的那样,在编辑失败或取消后,TextInputLayout不会取消选择。我尝试了不同的方法和方法,但没有人可以提供帮助。

pic - 1 pic - 2 pic - 3

另外,我打电话给

mFragmentProfileBinding.fragmentProfileNameTextInputLayout.clearFocus();

但我也没有工作。

<layout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools">

   <data>

       <variable
           name="viewModel"
           type="ir.basamadazmanovin.heartrate.ui.main.profile.ProfileViewModel" />

   </data>


   <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
       android:id="@+id/activity_login_swipe_refresh_layout"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       app:refreshing="@{viewModel.isLoading}">

       <androidx.core.widget.NestedScrollView
           android:layout_width="match_parent"
           android:layout_height="match_parent">


           <androidx.constraintlayout.widget.ConstraintLayout
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:background="@color/md_grey_200">

               <androidx.cardview.widget.CardView
                   android:id="@+id/fragment_profile_pro_cardView"
                   android:layout_width="0dp"
                   android:layout_height="0dp"
                   android:layout_marginStart="8dp"
                   android:layout_marginEnd="8dp"
                   app:layout_constraintBottom_toTopOf="@+id/guideline18"
                   app:layout_constraintEnd_toStartOf="@+id/guideline13"
                   app:layout_constraintStart_toStartOf="@+id/guideline12"
                   app:layout_constraintTop_toTopOf="@+id/guideline17">

                   <androidx.constraintlayout.widget.ConstraintLayout
                       android:layout_width="match_parent"
                       android:layout_height="match_parent">

                       <TextView
                           android:id="@+id/fragment_profile_account_status_textView"
                           android:layout_width="wrap_content"
                           android:layout_height="wrap_content"
                           android:layout_marginStart="8dp"
                           android:layout_marginTop="8dp"
                           android:layout_marginEnd="8dp"
                           android:layout_marginBottom="8dp"
                           android:text="Free Account"
                           app:layout_constraintBottom_toBottomOf="parent"
                           app:layout_constraintEnd_toStartOf="@+id/guideline21"
                           app:layout_constraintStart_toStartOf="parent"
                           app:layout_constraintTop_toTopOf="parent" />

                       <androidx.constraintlayout.widget.Guideline
                           android:id="@+id/guideline21"
                           android:layout_width="wrap_content"
                           android:layout_height="wrap_content"
                           android:orientation="vertical"
                           app:layout_constraintGuide_percent="0.5" />

                       <mehdi.sakout.fancybuttons.FancyButton
                           android:id="@+id/fragment_profile_premium_btn"
                           android:layout_width="124dp"
                           android:layout_height="wrap_content"
                           android:layout_marginStart="8dp"
                           android:layout_marginEnd="8dp"
                           android:onClick="@{()-> viewModel.onPremiumClick()}"
                           android:paddingStart="16dp"
                           android:paddingTop="8dp"
                           android:paddingEnd="16dp"
                           android:paddingBottom="8dp"
                           app:fb_borderColor="#FFFFFF"
                           app:fb_borderWidth="1dp"
                           app:fb_defaultColor="@color/button_color"
                           app:fb_focusColor="@color/button_color"
                           app:fb_radius="@dimen/button_radius"
                           app:fb_text="Premium"
                           app:fb_textColor="#FFFFFF"
                           app:fb_textSize="@dimen/button_textSize"
                           app:layout_constraintBottom_toBottomOf="@+id/fragment_profile_account_status_textView"
                           app:layout_constraintEnd_toEndOf="parent"
                           app:layout_constraintStart_toStartOf="@+id/guideline21"
                           app:layout_constraintTop_toTopOf="@+id/fragment_profile_account_status_textView" />


                   </androidx.constraintlayout.widget.ConstraintLayout>
               </androidx.cardview.widget.CardView>

               <View
                   android:id="@+id/fragment_profile_view"
                   android:layout_width="0dp"
                   android:layout_height="0dp"
                   android:background="@color/material_blue400"
                   android:visibility="visible"
                   app:layout_constraintBottom_toTopOf="@+id/guideline19"
                   app:layout_constraintEnd_toEndOf="parent"
                   app:layout_constraintHorizontal_bias="1.0"
                   app:layout_constraintStart_toStartOf="parent"
                   app:layout_constraintTop_toTopOf="parent"
                   app:layout_constraintVertical_bias="0.675" />

               <de.hdodenhof.circleimageview.CircleImageView
                   android:id="@+id/fragment_profile_profile_ImageView"
                   android:layout_width="124dp"
                   android:layout_height="124dp"
                   android:layout_marginTop="8dp"
                   android:layout_marginEnd="8dp"
                   android:layout_marginBottom="8dp"
                   android:src="@mipmap/ic_launcher_round"
                   app:civ_border_color="@color/material_white"
                   app:civ_border_width="2dp"
                   app:layout_constraintBottom_toTopOf="@+id/guideline17"
                   app:layout_constraintEnd_toStartOf="@+id/guideline20"
                   app:layout_constraintStart_toStartOf="@+id/guideline20"
                   app:layout_constraintTop_toTopOf="@+id/guideline14"
                   app:layout_constraintVertical_bias="0.358" />

               <TextView
                   android:id="@+id/fragment_profile_profile_textview"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:layout_marginTop="8dp"
                   android:layout_marginBottom="8dp"
                   android:text="@{viewModel.profileModelObservableField.name}"
                   android:textColor="@color/material_white"
                   app:layout_constraintBottom_toTopOf="@+id/guideline17"
                   app:layout_constraintEnd_toEndOf="@+id/fragment_profile_profile_ImageView"
                   app:layout_constraintStart_toStartOf="@+id/fragment_profile_profile_ImageView"
                   app:layout_constraintTop_toBottomOf="@+id/fragment_profile_profile_ImageView"
                   tools:text="oy" />

               <androidx.cardview.widget.CardView
                   android:layout_width="0dp"
                   android:layout_height="wrap_content"
                   android:layout_marginTop="8dp"
                   android:layout_marginBottom="8dp"
                   app:layout_constraintBottom_toBottomOf="parent"
                   app:layout_constraintEnd_toEndOf="@+id/fragment_profile_pro_cardView"
                   app:layout_constraintStart_toStartOf="@+id/fragment_profile_pro_cardView"
                   app:layout_constraintTop_toTopOf="@+id/guideline18">

                   <androidx.constraintlayout.widget.ConstraintLayout
                       android:layout_width="match_parent"
                       android:layout_height="match_parent">

                       <com.google.android.material.textfield.TextInputLayout
                           android:id="@+id/fragment_profile_name_textInputLayout"
                           style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                           android:layout_width="match_parent"
                           android:layout_height="wrap_content"
                           android:layout_marginStart="8dp"
                           android:layout_marginTop="8dp"
                           android:layout_marginEnd="8dp"
                           android:layout_marginBottom="8dp"
                           app:errorEnabled="true"
                           android:focusable="true"
                           app:layout_constraintBottom_toBottomOf="parent"
                           app:layout_constraintEnd_toEndOf="parent"
                           app:layout_constraintStart_toStartOf="parent"
                           app:layout_constraintTop_toTopOf="parent"
                           app:layout_constraintVertical_bias="0.0">

                           <com.google.android.material.textfield.TextInputEditText
                               android:id="@+id/fragment_profile_name_edit_text"
                               android:layout_width="match_parent"
                               android:layout_height="wrap_content"
                               android:hint="First Name"
                               tools:visibility="visible" />
                       </com.google.android.material.textfield.TextInputLayout>

                       <com.google.android.material.textfield.TextInputLayout
                           android:id="@+id/fragment_profile_lastname_textInputLayout"
                           style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                           android:layout_width="match_parent"
                           android:layout_height="wrap_content"
                           android:layout_marginStart="8dp"
                           android:layout_marginTop="4dp"
                           android:layout_marginEnd="8dp"
                           android:layout_marginBottom="8dp"
                           app:errorEnabled="true"
                           app:layout_constraintBottom_toBottomOf="parent"
                           app:layout_constraintEnd_toEndOf="@+id/fragment_profile_name_textInputLayout"
                           app:layout_constraintStart_toStartOf="@+id/fragment_profile_name_textInputLayout"
                           app:layout_constraintTop_toBottomOf="@+id/fragment_profile_name_textInputLayout"
                           app:layout_constraintVertical_bias="0.0">

                           <com.google.android.material.textfield.TextInputEditText
                               android:id="@+id/fragment_profile_lastname_edit_text"
                               android:layout_width="match_parent"
                               android:layout_height="wrap_content"
                               android:hint="Last Name" />
                       </com.google.android.material.textfield.TextInputLayout>

                       <com.google.android.material.textfield.TextInputLayout
                           android:id="@+id/fragment_profile_year_textInputLayout"
                           style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                           android:layout_width="0dp"
                           android:layout_height="wrap_content"
                           android:layout_marginStart="8dp"
                           android:layout_marginTop="4dp"
                           android:layout_marginEnd="8dp"
                           android:layout_marginBottom="8dp"
                           app:errorEnabled="true"
                           app:layout_constraintBottom_toBottomOf="parent"
                           app:layout_constraintEnd_toStartOf="@+id/fragment_profile_month_textInputLayout"
                           app:layout_constraintHorizontal_bias="0.5"
                           app:layout_constraintStart_toStartOf="parent"
                           app:layout_constraintTop_toBottomOf="@+id/fragment_profile_lastname_textInputLayout"
                           app:layout_constraintVertical_bias="0.0">

                           <com.google.android.material.textfield.TextInputEditText
                               android:id="@+id/fragment_profile_year_edit_text"
                               android:layout_width="match_parent"
                               android:layout_height="wrap_content"
                               android:hint="Year" />
                       </com.google.android.material.textfield.TextInputLayout>

                       <com.google.android.material.textfield.TextInputLayout
                           android:id="@+id/fragment_profile_month_textInputLayout"
                           style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                           android:layout_width="0dp"
                           android:layout_height="wrap_content"
                           android:layout_marginTop="4dp"
                           android:layout_marginBottom="8dp"
                           app:errorEnabled="true"
                           app:layout_constraintBottom_toBottomOf="parent"
                           app:layout_constraintEnd_toStartOf="@+id/fragment_profile_day_textInputLayout"
                           app:layout_constraintHorizontal_bias="0.5"
                           app:layout_constraintStart_toEndOf="@+id/fragment_profile_year_textInputLayout"
                           app:layout_constraintTop_toBottomOf="@+id/fragment_profile_lastname_textInputLayout"
                           app:layout_constraintVertical_bias="0.0">

                           <com.google.android.material.textfield.TextInputEditText
                               android:id="@+id/fragment_profile_month_edit_text"
                               android:layout_width="match_parent"
                               android:layout_height="wrap_content"
                               android:hint="Month" />
                       </com.google.android.material.textfield.TextInputLayout>

                       <com.google.android.material.textfield.TextInputLayout
                           android:id="@+id/fragment_profile_day_textInputLayout"
                           style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                           android:layout_width="0dp"
                           android:layout_height="wrap_content"
                           android:layout_marginStart="8dp"
                           android:layout_marginTop="4dp"
                           android:layout_marginEnd="8dp"
                           android:layout_marginBottom="8dp"
                           app:errorEnabled="true"
                           app:layout_constraintBottom_toBottomOf="parent"
                           app:layout_constraintEnd_toEndOf="parent"
                           app:layout_constraintHorizontal_bias="0.5"
                           app:layout_constraintStart_toEndOf="@+id/fragment_profile_month_textInputLayout"
                           app:layout_constraintTop_toBottomOf="@+id/fragment_profile_lastname_textInputLayout"
                           app:layout_constraintVertical_bias="0.0">

                           <com.google.android.material.textfield.TextInputEditText
                               android:id="@+id/fragment_profile_day_edit_text"
                               android:layout_width="match_parent"
                               android:layout_height="wrap_content"
                               android:hint="Day" />
                       </com.google.android.material.textfield.TextInputLayout>

                       <com.google.android.material.textfield.TextInputLayout
                           android:id="@+id/fragment_profile_description_textInputLayout"
                           style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                           android:layout_width="0dp"
                           android:layout_height="wrap_content"
                           android:layout_marginTop="4dp"
                           android:layout_marginBottom="8dp"
                           app:errorEnabled="true"
                           app:layout_constraintBottom_toBottomOf="parent"
                           app:layout_constraintEnd_toEndOf="@+id/fragment_profile_day_textInputLayout"
                           app:layout_constraintStart_toStartOf="@+id/fragment_profile_year_textInputLayout"
                           app:layout_constraintTop_toBottomOf="@+id/fragment_profile_month_textInputLayout"
                           app:layout_constraintVertical_bias="0.0">

                           <com.google.android.material.textfield.TextInputEditText
                               android:id="@+id/fragment_profile_description_edit_text"
                               android:layout_width="match_parent"
                               android:layout_height="128dp"
                               android:gravity="top"
                               android:hint="Describe Your Problems Here"
                               android:inputType="textMultiLine" />
                       </com.google.android.material.textfield.TextInputLayout>

                   </androidx.constraintlayout.widget.ConstraintLayout>
               </androidx.cardview.widget.CardView>

               <androidx.constraintlayout.widget.Guideline
                   android:id="@+id/guideline12"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:orientation="vertical"
                   app:layout_constraintGuide_begin="8dp" />

               <androidx.constraintlayout.widget.Guideline
                   android:id="@+id/guideline13"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:orientation="vertical"
                   app:layout_constraintGuide_end="8dp" />

               <androidx.constraintlayout.widget.Guideline
                   android:id="@+id/guideline14"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:orientation="horizontal"
                   app:layout_constraintGuide_begin="8dp" />

               <androidx.constraintlayout.widget.Guideline
                   android:id="@+id/guideline16"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:orientation="horizontal"
                   app:layout_constraintGuide_percent="0.5" />

               <androidx.constraintlayout.widget.Guideline
                   android:id="@+id/guideline17"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:orientation="horizontal"
                   app:layout_constraintGuide_percent="0.3" />

               <androidx.constraintlayout.widget.Guideline
                   android:id="@+id/guideline18"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:orientation="horizontal"
                   app:layout_constraintGuide_percent="0.4" />

               <androidx.constraintlayout.widget.Guideline
                   android:id="@+id/guideline19"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:orientation="horizontal"
                   app:layout_constraintGuide_percent="0.35" />

               <androidx.constraintlayout.widget.Guideline
                   android:id="@+id/guideline20"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:orientation="vertical"
                   app:layout_constraintGuide_percent="0.5" />

               <androidx.constraintlayout.widget.Guideline
                   android:id="@+id/guideline22"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:orientation="horizontal"
                   app:layout_constraintGuide_percent="0.45" />

           </androidx.constraintlayout.widget.ConstraintLayout>

       </androidx.core.widget.NestedScrollView>
   </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

</layout>

public class ProfileFragment
       extends BaseFragment<FragmentProfileBinding,ProfileViewModel> implements ProfileNavigator {

   @Inject
   ViewModelProviderFactory factory;
   private ActionMode mActionMode;
   private FragmentProfileBinding mFragmentProfileBinding;
   private ProfileViewModel mProfileViewModel;


   public ProfileFragment() {
       // Required empty public constructor
   }



   public static ProfileFragment newInstance() {
       ProfileFragment fragment = new ProfileFragment();
       Bundle args = new Bundle();
//        args.putString(ARG_PARAM1, param1);
//        args.putString(ARG_PARAM2, param2);
       fragment.setArguments(args);
       return fragment;
   }

   @Override
   public int getBindingVariable() {
       return BR.viewModel;
   }

   @Override
   public int getLayoutId() {
       return R.layout.fragment_profile;
   }

   @Override
   public ProfileViewModel getViewModel() {
       mProfileViewModel= ViewModelProviders.of(this, factory).get(ProfileViewModel.class);
       return mProfileViewModel;
   }

   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       mProfileViewModel.setNavigator(this);

   }



   @Override
   public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
       super.onViewCreated(view, savedInstanceState);
       mFragmentProfileBinding = getViewDataBinding();

       subscribeToProfile();
       deActiveEditTexts();
   }

   private void subscribeToProfile(){
       mProfileViewModel.getProfileModelMutableLiveData().observe(this,
               profileModel ->
                       mProfileViewModel.setProfileModelObservableField(profileModel));

   }



   @Override
   public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
       inflater.inflate(R.menu.fragment_profile_menu, menu);
       super.onCreateOptionsMenu(menu, inflater);
   }


   @Override
   public boolean onOptionsItemSelected(@NonNull MenuItem item) {
       switch (item.getItemId()) {

           case R.id.fragment_profile_menu_edit:
               mActionMode = getBaseActivity().startSupportActionMode(new ProfileFragmentActionMode(this));
               activeEditTexts();
               return true;

           default:
               break;
       }

       return false;
   }

   private void setUp(){

   }
   @Override
   public void editProfileInfo() {
       deActiveEditTexts();
       setNullToActionMode();
       Toast.makeText(getActivity(),"oy",Toast.LENGTH_SHORT).show();

   }

   @Override
   public void cancelProfileInfo() {
       deActiveEditTexts();
       setNullToActionMode();
   }

   private void activeEditTexts(){
       mFragmentProfileBinding.fragmentProfileNameEditText.setEnabled(true);
       mFragmentProfileBinding.fragmentProfileLastnameEditText.setEnabled(true);
       mFragmentProfileBinding.fragmentProfileYearEditText.setEnabled(true);
       mFragmentProfileBinding.fragmentProfileMonthEditText.setEnabled(true);
       mFragmentProfileBinding.fragmentProfileDayEditText.setEnabled(true);
       mFragmentProfileBinding.fragmentProfileDescriptionEditText.setEnabled(true);
       mFragmentProfileBinding.fragmentProfileNameEditText.requestFocus();

   }
   private void deActiveEditTexts(){

       mFragmentProfileBinding.fragmentProfileNameTextInputLayout.clearFocus();
       mFragmentProfileBinding.fragmentProfileLastnameTextInputLayout.clearFocus();
       mFragmentProfileBinding.fragmentProfileYearTextInputLayout.clearFocus();
       mFragmentProfileBinding.fragmentProfileMonthTextInputLayout.clearFocus();
       mFragmentProfileBinding.fragmentProfileDayTextInputLayout.clearFocus();
       mFragmentProfileBinding.fragmentProfileDescriptionTextInputLayout.clearFocus();


       mFragmentProfileBinding.fragmentProfileNameEditText.setEnabled(false);
       mFragmentProfileBinding.fragmentProfileLastnameEditText.setEnabled(false);
       mFragmentProfileBinding.fragmentProfileYearEditText.setEnabled(false);
       mFragmentProfileBinding.fragmentProfileMonthEditText.setEnabled(false);
       mFragmentProfileBinding.fragmentProfileDayEditText.setEnabled(false);
       mFragmentProfileBinding.fragmentProfileDescriptionEditText.setEnabled(false);
   }

   //Set action mode null after use
   public void setNullToActionMode() {

       if (mActionMode != null){
           mActionMode.finish();
           mActionMode = null;
       }

   }


   @Override
   public void onBackPressed() {
//        deActiveEditTexts();
//        cancelProfileInfo();
   }

   @Override
   public void handleError(Throwable throwable) {

   }



}
android android-edittext android-databinding android-textinputlayout android-textinputedittext
4个回答
1
投票

好的,现在你要禁用编辑文本

editTextName.setFocusable(false);
editTextName.setEnabled(false);

或者当您要在用户单击时启用编辑文本时

editTextName.setFocusable(true);
editTextName.setFocusableInTouchMode(true);
editTextName.setEnabled(true);
editTextName.requestFocus();

希望它会对你有所帮助。


1
投票

尝试在LinearLayout之前将TextInputLayout添加到您的XML:

<LinearLayout
    android:id="@+id/thief_layout"
    android:focusable="true"
    android:focusableInTouchMode="true" 
    android:clickable="true"
    android:layout_width="0px"
    android:layout_height="0px" />

然后将此代码添加到onBackPressed()片段:

editText.clearFocus();
thiefLayout.requestFocus();

1
投票

试试这个

editText.setFocusableInTouchMode(false);

editText.setFocusable(false);

editText.setFocusableInTouchMode(true);

editText.setFocusable(true);

0
投票

你应该使用接口来使用你无法直接在片段中注册example thread is here

© www.soinside.com 2019 - 2024. All rights reserved.