您的位置:首页 > 其它

让edittext不自动获取焦点入坑与出坑记

2016-07-13 15:37 316 查看
先看代码再看套路

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

<include layout="@layout/activity_top_back_title_more" />

<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<!-- -->

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:orientation="vertical" >

<EditText
android:id="@+id/et_number"
style="@style/BlueStrokeEdit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="사용자명을 입력하십시오"
android:singleLine="true" />

<EditText
android:id="@+id/et_password"
style="@style/BlueStrokeEdit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:hint="@string/input_password"
android:inputType="textWebPassword"
android:singleLine="true"
android:textSize="16sp" />

<TextView
android:id="@+id/btn_login"
style="@style/BlueButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:clickable="true"
android:gravity="center"
android:text="@string/login" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="40dp"
android:orientation="horizontal" >

<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:background="#DDDCDB" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:gravity="center"
android:text="@string/wxlogin"
android:textColor="#DDDCDB" />

<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:background="#DDDCDB" />
</LinearLayout>

<ImageView
android:id="@+id/loginbywx"
style="@style/BlueButton"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:layout_marginTop="40dp"
android:background="@drawable/loginbywechat"
android:clickable="true"
android:contentDescription="@string/wxlogin"
android:scaleType="centerInside" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center"
android:text="@string/fastlogin"
android:textColor="#DDDCDD" />
</LinearLayout>
</ScrollView>

</LinearLayout>java代码部分
et_number.setOnFocusChangeListener(new OnFocusChangeListener() {

@Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO 清除hint
if (hasFocus) {

et_number.setHint("");

} else {
et_number.setHint("사용자명을 입력하십시오");

}
}
});
et_password.setOnFocusChangeListener(new OnFocusChangeListener() {

@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
et_password.setHint("");
} else {
et_password.setHint(getString(R.string.input_password));

}
}
});

网友们都说  ,在edittext的父布局去加上
android:focusable="true"
android:focusableInTouchMode="true"
就好了,我加上之后没卵用,之后又各种改,都没达到要求,最终试了一下 ,放到最外层布局,嗯 就是这样  。不多说,我是红领巾
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: