当我点击其他区域时,如何让edittext失去焦点?

时间:2015-01-08 13:34:27

标签: android

我在谷歌遇到过类似的问题,但我不知道该怎么办?这是我的布局,我设置了两个edittext和一个按钮。 Edittext用于输入用户名和密码。我想关闭键盘,当我点击屏幕上的其他区域时,让按钮获得焦点。现在,当我点击按钮时,它无法进入任何活动!我认为这是因为按钮没有得到关注。我对吗?谁能帮帮我?

<RelativeLayout
    android:id="@+id/rl_user"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/login_picture" >

    <LinearLayout
        android:id="@+id/ll_user_info"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="5dp"
        android:background="@drawable/login_input_bg"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/ll_username"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal" >

            <ImageView
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_gravity="center_vertical"
                android:contentDescription="@string/login_username"
                android:padding="5dp"
                android:scaleType="fitXY"
                android:src="@drawable/login_username" />

            <EditText
                android:id="@+id/edit_username"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_margin="2dp"
                android:background="#000000ff"
                android:hint="@string/login_username"
                android:inputType="text"
                android:padding="5dp"
                android:textColor="#ffffff"
                android:textColorHint="#ffffff" />
        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="0.5dp"
            android:background="@color/devide_line" />

        <LinearLayout
            android:id="@+id/ll_pwd"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal" >

            <ImageView
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_gravity="center_vertical"
                android:contentDescription="@string/login_username"
                android:padding="5dp"
                android:scaleType="fitXY"
                android:src="@drawable/login_password" />

            <EditText
                android:id="@+id/edit_pwd"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_margin="2dp"
                android:background="#000000ff"
                android:hint="@string/login_pwd"
                android:inputType="textPassword"
                android:padding="5dp"
                android:textColor="#ffffff"
                android:textColorHint="#ffffff" />
        </LinearLayout>
    </LinearLayout>

    <Button
        android:id="@+id/btn_login"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_below="@id/ll_user_info"
        android:layout_marginLeft="25dp"
        android:layout_marginRight="25dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/login_btn"
        android:text="@string/login"
        android:textColor="@android:color/white" />
</RelativeLayout>

2 个答案:

答案 0 :(得分:1)

我不知道重点是否是真正的问题,但是您可以尝试使用this snippet代码来删除editext焦点

编辑:我将代码段链接修改为this

答案 1 :(得分:0)

尝试设置:

android:clickable="true" 
android:focusable="true" 
to other view and layout in your xml.
if you don't have result try to hidden keyboard

EditText txtEdit = (EditText) findViewById(R.id.edittxt);
txtEdit.setOnFocusChangeListener(new OnFocusChangeListener() {          
public void onFocusChange(View v, boolean hasFocus) {
            if (!hasFocus) {
hideKeyboard();
               // code to execute when EditText loses focus
            }
        }
    });
private void hideKeyboard() { 
// Check if no view has focus:
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}