今天在做一个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一位大佬的,但关掉之后找不到了原文了,,,在这里对大佬表示衷心的感谢。