gpt4 book ai didi

java - 按钮 onClick() 导致强制停止

转载 作者:行者123 更新时间:2023-12-02 10:51:01 25 4
gpt4 key购买 nike

StackOverFlowGeeks 大家好!目前,我正在学习如何编写一个基本的 Android 应用程序,该应用程序几乎没有按钮。我有四个 Activity (Java 类)和四个 xml。

我有 RegisterScreen.java,它有两个按钮(注册和后退按钮)。目前,我正在尝试从 RegisterScreen.java 返回到 SignInScreen.java。问题是,当我单击后退按钮时,它会导致失败并且应用程序被迫关闭......所以我是这样上传的: 1)注册屏幕.java 2)SignInScreen.java 3)注册屏幕XML文件 4) 登录屏幕 XML 文件提前致谢。

注册屏幕:

public class RegisterScreen extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register_screen);
}


public void onClickRegisterButton(View view){

}

public void onClickBackButton(View view) {
Button backBtn = (Button) findViewById(R.id.backBtn);
backBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(RegisterScreen.this, SignInScreen.class);
RegisterScreen.this.startActivity(intent);
}
});
}
}

登录屏幕:

public class SignInScreen extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first_screen);
}

@SuppressLint("SetTextI18n")
public void onClickSignInButton(View view){

TextView username = (TextView)findViewById(R.id.username_text);
TextView password = (TextView)findViewById(R.id.password_text);
Button signIn = (Button)findViewById(R.id.sign_in_button);

if (username.getText().toString().equals("Ivan Simeonov") && password.getText().toString().equals("Ivan9603116245")){
signIn.setText("Welcome " + username.getText().toString());
signIn.setBackgroundColor(Color.YELLOW);
setContentView(R.layout.activity_logged_screen);
}else{
signIn.setText("The input data is incorrect! Try again!");
signIn.setBackgroundColor(Color.GREEN);
}
}

public void onClickRegisterButton(View view){
Button register = (Button)findViewById(R.id.register_button);
setContentView(R.layout.activity_register_screen);
}

public void onClickResetPasswordButton(View view){
Button resetPassword = (Button)findViewById(R.id.reset_password_button);
setContentView(R.layout.activity_reset_password_screen);
}
}

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context="com.example.megat0n.startproject.RegisterScreen">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:id="@+id/textView_Register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/register_your_account_by_filling_up_the_following_data"
android:textAlignment="center"
android:textColor="@android:color/black"
android:textSize="24sp"
android:textStyle="bold" />

<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/enter_username"
android:inputType="textPersonName" />

<EditText
android:id="@+id/first_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/enter_firstname"
android:inputType="textPersonName" />

<EditText
android:id="@+id/last_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/enter_lastname"
android:inputType="textPersonName" />

<EditText
android:id="@+id/birth_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/enter_birthdate"
android:inputType="date" />

<EditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/enter_email"
android:inputType="textPersonName" />

<EditText
android:id="@+id/email_confirm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/confirm_email"
android:inputType="textPersonName" />

<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/enter_password"
android:inputType="textPassword" />

<EditText
android:id="@+id/password_confirm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/confirm_password"
android:inputType="textPassword" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<Button
android:id="@+id/reg_account"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/register"
android:onClick="onClickRegisterButton"
android:textAlignment="center"
android:textAllCaps="false"
android:textSize="18sp"
android:textStyle="bold" />

<Button
android:id="@+id/backBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/back"
android:onClick="onClickBackButton"
android:textAlignment="center"
android:textAllCaps="false"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>

</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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"
tools:context="com.example.megat0n.startproject.SignInScreen">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="56dp"
android:text="@string/welcome_to_the_home_screen"
android:textAlignment="center"
android:textColor="@android:color/black"
android:textSize="24sp"
android:textStyle="bold" />

<EditText
android:id="@+id/username_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/username"
android:inputType="textPersonName"
android:textAlignment="center"
android:textStyle="bold" />

<EditText
android:id="@+id/password_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/password"
android:inputType="textPassword"
android:textAlignment="center"
android:textStyle="bold" />

<Button
android:id="@+id/sign_in_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClickSignInButton"
android:text="@string/sign_in"
android:textAlignment="center"
android:textAllCaps="false"
android:textSize="18sp"
android:textStyle="bold" />

<Button
android:id="@+id/register_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClickRegisterButton"
android:text="@string/register"
android:textAlignment="center"
android:textAllCaps="false"
android:textSize="18sp"
android:textStyle="bold" />

<Button
android:id="@+id/reset_password_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClickResetPasswordButton"
android:text="@string/reset_password"
android:textAlignment="center"
android:textAllCaps="false"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>

最佳答案

使用这个 onClickBackButton() 而不是你的。如果您在布局中使用 onClick 属性,则不需要创建按钮和单击监听器。

public void onClickBackButton(View view) {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
}

关于java - 按钮 onClick() 导致强制停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52187847/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com