登录时if语句出现问题,android [英] a problem with if statement when login, android
本文介绍了登录时if语句出现问题,android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好:)
我的代码有问题
当我尝试在没有数据库的情况下登录时,它没有任何作用
这是.Java文件
hi all :)
I have a problem with this code
when I try to login without database, it make no action
here is the .Java File
package com.msagroup.sbhmg;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class LogIn extends Activity {
Button _gotousermain;
EditText _etmail;
EditText _etpassword;
String _email;
String _pass;
TextView _tvcomm;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
_gotousermain = (Button) findViewById(R.id.btnlogin);
_etmail = (EditText) findViewById(R.id.etmail);
_etpassword = (EditText) findViewById(R.id.etpassword);
_tvcomm = (TextView) findViewById(R.id.tvcomment);
_gotousermain.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//startActivity(new Intent(LogIn.this, UserMain.class));
_email = _etmail.getText().toString();
_pass = _etpassword.getText().toString();
if(_email == "user@user.com" && _pass == "123")
{
startActivity(new Intent(LogIn.this, UserMain.class));
}
else
{
if(_email == "admin@admin.com" && _pass == "123")
{
startActivity(new Intent(LogIn.this, AdminMain.class));
}
else
{
_tvcomm.append("@string");
}
}
//}
//catch (Exception e) {
//}
//finally
//{
//
//}
}
});
}
}
这是.XML文件:
and here is the .XML file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="@+id/etmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="41dp"
android:hint="@string/email"
android:inputType="textEmailAddress" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/etpassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/etmail"
android:layout_marginTop="16dp"
android:hint="@string/password"
android:inputType="textPassword" />
<Button
android:id="@+id/btnlogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/etpassword"
android:layout_marginTop="16dp"
android:text="@string/login" />
<Button
android:id="@+id/btnregistration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/btnlogin"
android:layout_marginTop="70dp"
android:text="@string/registration" />
<Button
android:id="@+id/btnbacktomain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btnregistration"
android:layout_alignBottom="@+id/btnregistration"
android:layout_alignParentRight="true"
android:text="@string/backtomain" />
<TextView
android:id="@+id/tvcomment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="@string/invalidlogin"
android:visibility="invisible"/>
</RelativeLayout>
谢谢大家:)
thank you all :)
推荐答案
用此替换您的If语句
Replace your If statement with this
if(_email.equals("user@user.com") && _pass.equals("123")
我们仅在int值中使用==,对于字符串,我们使用.equals
在您的代码中尝试一下.
We use == only in int values and for string we use .equals
Try this in your code.
查看此 ^ ]
这篇关于登录时if语句出现问题,android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文