2016-05-18 2 views
0

У меня есть два EditText s и один CheckBox и Button в моем оформлении в вышеуказанном порядке. После ввода значений в EditText пользователь должен принять условия и положения, нажав Checkbox. Мне нужно удалить фокус с EditText с после щелчка по галочке. Прямо сейчас фокус всегда находится на втором EditText. Как это можно достичь? Пожалуйста помоги. Layout: Удалить фокус с EditText в Android

<EditText 
     android:id="@+id/pm_et_customer_id" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:hint="@string/customer_id_hint" 
     android:fontFamily="sans-serif-light" 
     android:gravity="center" 
     android:singleLine="true" 
     android:layout_centerHorizontal="true" 
     android:inputType="phone" 
     android:textAppearance="?android:attr/textAppearanceMedium"/> 

    <RelativeLayout 
     android:id="@+id/pm_rl_mob_no_widget" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:padding="6dp" 
     android:layout_below="@id/pm_et_customer_id" 
     android:gravity="center_horizontal"> 

    <EditText 
     android:id="@+id/pm_et_dial_code" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/pm_et_msisdn" 
     android:layout_marginLeft="5dp" 
     android:ems="3" 
     android:fontFamily="sans-serif-thin" 
     android:gravity="center" 
     android:text="@string/plus_nine_one" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
    /> 

    <EditText 
     android:id="@+id/pm_et_msisdn" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@+id/pm_et_dial_code" 
     android:ems="9" 
     android:fontFamily="sans-serif-light" 
     android:gravity="center_horizontal" 
     android:hint="@string/msisdn_hint" 
     android:inputType="phone" 
     android:maxLength="14"/> 
    </RelativeLayout> 

    <Button 
     android:id="@+id/pm_bt_proceed" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/lay_t_c" 
     android:layout_centerHorizontal="true" 
     android:layout_marginLeft="20dp" 
     android:layout_marginRight="20dp" 
     android:layout_marginTop="8dp" 
     android:fontFamily="sans-serif-light" 
     android:text="@string/bt_label_proceed"/> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/pm_rl_mob_no_widget" 
     android:id="@+id/lay_t_c" 
     android:layout_marginTop="10dp" 
     android:gravity="center" 
     android:orientation="horizontal"> 
    <CheckBox 
     android:id="@+id/pm_check_t_and_c" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:fontFamily="sans-serif-thin" 
     android:textSize="14sp" 
     android:text="@string/pm_label_accept"/> 

    <TextView 
     android:id="@+id/pm_tv_t_and_c" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical" 
     android:layout_marginRight="4dp" 
     android:layout_marginEnd="4dp" 
     android:paddingLeft="4dp" 
     android:paddingStart="4dp" 
     android:fontFamily="sans-serif-light" 
     android:text="@string/pm_label_t_and_c" 
     android:textSize="14sp"/> 
    </LinearLayout> 

</RelativeLayout> 
+0

Просто ясное ваше внимание на 'Check Box' мыши. –

+0

@bearded beast, что представляет собой последовательность ваших просмотров в макете? – Dhiraj

+0

@jaydroider с его фокусировкой на первый EditText –

ответ

-1
editText.setEnabled(false); 

Используйте его на Checkbox он работал у слушателя для меня

+0

Если я это сделаю, я не смогу отредактировать его снова. –

+0

перед редактированием поместите эту строку editText.setEnabled (false); – Haroon

0
checkBox.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      editText.setFocusable(false); 
     } 
    }); 

Добавить выше OnClickListener проверить коробку.

+0

Если я это сделаю, я не смогу снова отредактировать EditText. –

+1

set 'android: focusableInTouchMode =" true "' для большинства внешних макетов. и назовите 'editText.clearFocus();' внутри флажок OnClickListener. Работает на меня. –

0
checkBox.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      editText.setFocusable(false); 
     } 
    }); 

После удаления фокуса, пожалуйста, сделайте еще один ..

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

0

Использование onCheckedChangeListener из Check Box вы можете сделать это. Просто применять это на Activity's onCreate()

chb1 = (CheckBox) findViewById(R.id.pm_check_t_and_c); 
     chb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       if (chb1.isChecked()) { 
        edt1.clearFocus(); 
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
        imm.hideSoftInputFromWindow(edt1.getWindowToken(), 0); 
       } else { 
        edt1.requestFocus(); 
       } 

      } 
     }); 
+0

Он работает или нет? –

Смежные вопросы