关于在安卓中使用String获取EditText输入框文字会将android:text的内容一并获取

今天在做一个app登录界面时,发现if语句的判断条件一直出问题,不能反馈登录

后面经过不断测试,发现问题出在这

String user = name.getText().toString().trim();
String pwd = passwd.getText().toString().trim();

当然这么写是没问题的,但是,问题就出在,你用这条语句的时候,它会把你EditText中的Android:text中的内容也给读取了,所以我的密码设置为1,结果if语句判断的确是 密码:1

所以肯定不匹配。

下面我把密码pwd用toast展示了一下

对于如何解决,我目前是没找到非常完美的方案,于是给账号和密码分别写了一个TextView

<TextView
    android:id="@+id/zhang"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="text"
    android:text="账号:"
    android:textColor="@color/black">


</TextView>
<EditText
    android:id="@+id/name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="text"
    android:textColor="@color/black"
    ></EditText>


<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="129dp"
    android:inputType="text"
    android:text="密码:"
    android:textColor="@color/black">


</TextView><EditText
android:id="@+id/passwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="118dp"
android:inputType="text"
android:textColor="@color/black"></EditText>

最后呢,也是成功实现了登录。

登录界面源代码是借鉴的csdn一位大佬的,但关掉之后找不到了原文了,,,在这里对大佬表示衷心的感谢。

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值