在Android中从EditText移除焦点 [英] Remove focus from a EditText in Android

查看:151
本文介绍了在Android中从EditText移除焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的布局中按上述顺序有两个EditText和一个CheckBox和一个Button.在将值输入到EditText之后,用户必须通过单击Checkbox接受条款和条件.单击复选框后,我需要从EditText移除焦点.现在,焦点始终放在第二个EditText上.如何做到这一点?请帮忙. 布局:

I have two EditTexts and one CheckBox and a Button in my layout in the above order. After entering the values to the EditText, the user has to accept terms and conditions by clicking the Checkbox. I need to remove focus from EditTexts after the checkbox is clicked. Right now the focus is always on the second EditText. How can this be achieved? Please help. Layout:

  <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>

推荐答案

使用Check Box中的onCheckedChangeListener,您可以执行此操作.只需将其应用于您的Activity's onCreate()

Using onCheckedChangeListener of Check Box you can do this. Just apply this on your 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();
                }

            }
        });

这篇关于在Android中从EditText移除焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆