是这样的,博主想做一个登陆页面,再输入用户名和密码的时候会弹出登陆成功字样。然而页面可以正常打开
但输入之后单击登陆按钮就会闪退
百度找了好多方法也没有解决,求助大神?
源码:login.java
package activity;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import net.hnjdzy.tinyaccount.R;
public class LoginActivity extends AppCompatActivity {
Button btnLogin;
EditText editTextName,editTextPwd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
btnLogin=(Button) this.findViewById(R.id.buttonLogin);//绑定按钮登录框
editTextName=(EditText) this.findViewById(R.id.editTextName);//绑定用户名登录框
editTextPwd=(EditText) this.findViewById(R.id.editTextTextPassword);//初始化控件
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Login();
}
});
}
//定义登录方法
private void Login(){
String name=editTextName.getText().toString(); //获取文本框输入的姓名
String pwd=editTextPwd.getText().toString();//获取文本框输入的密码
if(name.equals("admin")&&pwd.equals("admin")){
Toast.makeText(this,"登录成功",Toast.LENGTH_LONG).show();//弹出登录成功窗口,要用到Toast类的makeText方法
//三个信息,第一个上下文,第二个提示的信息,第三个提示显示的时间
}
else{
Toast.makeText(this,"登录失败",Toast.LENGTH_LONG).show();//弹出登录失败窗口
}
}
}
login.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="activity.LoginActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
app:srcCompat="@drawable/default_user_logo" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:ignore="ExtraText"
android:layout_marginTop="50dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
>
<TextView
android:id="@+id/textView11"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="用户昵称" />
<EditText
android:id="@+id/editTextName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:hint="请输入用户名" />
<TextView
android:id="@+id/textView13"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="密码" />
<EditText
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword"
android:hint="请输入密码"/>
<Button
android:id="@+id^ttonLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FF8833"
android:text="登录" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_marginTop="20dp"
>
<TextView
android:id="@+id/textViewNoPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/colorPrimary"
android:layout_weight="1"
android:layout_marginLeft="10dp"
android:text="忘记密码" />
<TextView
android:id="@+id/textViewRegister"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/colorPrimary"
android:layout_weight="1"
android:gravity="right"
android:layout_marginRight="10dp"
android:text="注册用户" />
</LinearLayout>
</LinearLayout>
谢谢
版权声明:本文为qq_48141992原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。