gpt4 book ai didi

java - onClick 监听器和 onClick on XML 将不起作用

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

所以我在尝试使用sharedPreferences进行登录/注册时遇到了问题,如果我在onCreate方法中使用了带有onCickListener的onCick,则该方法将不会运行,并且如果我在onCreate之外使用了该方法XML 上的方法调用将会崩溃。

带有 onClickListener 的完整 java 代码:

package com.example.mrsanchez.tunalogs;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Region;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class register extends AppCompatActivity {
//
//Variables:
//
public static final String MyPREFERENCES = "MyPrefs";
public static final String name = "nameKey";
public static final String email = "emailKey";
public static final String password = "passwordKey";
SharedPreferences sharedpreferences;
EditText edName;
EditText edPassword;
EditText edRepeat;
EditText edEmail;

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

sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

edName = (EditText) findViewById(R.id.txtUsername);
edPassword = (EditText) findViewById(R.id.txtPassword);
edRepeat = (EditText) findViewById(R.id.txtRepeat);
edEmail = (EditText) findViewById(R.id.txtEmail);
Button regist = (Button) findViewById(R.id.btnRegisterFinal);

regist.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String n = edName.getText().toString();
String p = edPassword.getText().toString();
String pr = edRepeat.getText().toString();
String e = edEmail.getText().toString();

SharedPreferences.Editor editor = sharedpreferences.edit();

if(p.equals(pr))
{
editor.putString(name, n);
editor.putString(password, p);
editor.putString(email, e);
editor.commit();
Toast.makeText(register.this,"Your account has been successfully created!",Toast.LENGTH_LONG).show();

}
else {
Toast.makeText(register.this, "Your passwords do not correspond.", Toast.LENGTH_LONG).show();
}
}
});
}

}

完整的 XML 代码:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_register"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.mrsanchez.tunalogs.register">

<EditText
android:id="@+id/txtUsername"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:layout_width="300dp"
android:layout_height="40dp"
android:hint="Username"/>

<EditText
android:id="@+id/txtPassword"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:layout_below="@+id/txtUsername"
android:layout_width="300dp"
android:layout_height="40dp"
android:hint="Password"
android:inputType="textPassword"/>

<EditText
android:id="@+id/txtRepeat"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:layout_below="@+id/txtPassword"
android:layout_width="300dp"
android:layout_height="40dp"
android:hint="Confirm Password"
android:inputType="textPassword"/>

<EditText
android:id="@+id/txtEmail"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:layout_below="@+id/txtRepeat"
android:layout_width="300dp"
android:layout_height="40dp"
android:hint="E-Mail"
android:inputType="textEmailAddress"
/>

<Button
android:id="@+id/btnRegisterFinal"
android:layout_width="130dp"
android:layout_height="60dp"
android:layout_below="@+id/txtEmail"
android:layout_marginTop="50dp"
android:layout_centerHorizontal="true"
android:text="Register"/>


</RelativeLayout>

当前 logcat(没有崩溃,但方法仍未运行):

10-18 14:44:48.681 16037-16037/? E/cutils: to chown(/mnt/shell/emulated/0, 0, 0)
10-18 14:44:48.681 16037-16037/? E/cutils: to chown(/mnt/shell/emulated/obb, 0, 0)
10-18 14:44:48.682 16037-16037/? E/cutils: to chown(/storage/emulated/0/Android, 0, 0)
10-18 14:44:48.682 16037-16037/? E/cutils: to chown(/storage/emulated/0/Android/obb, 0, 0)
10-18 14:44:48.804 16037-16037/com.example.mrsanchez.tunalogs E/dalvikvm: Could not find class 'android.util.ArrayMap', referenced from method com.android.tools.fd.runtime.MonkeyPatcher.monkeyPatchExistingResources
10-18 14:44:48.809 16037-16037/com.example.mrsanchez.tunalogs E/dalvikvm: Could not find class 'android.util.ArrayMap', referenced from method com.android.tools.fd.runtime.MonkeyPatcher.pruneResourceCache
10-18 14:44:48.912 16037-16037/com.example.mrsanchez.tunalogs E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering

请帮助我,我已经在堆栈溢出中徘徊了一段时间,并没有真正找到任何有用的东西,除非我过度关注或错过了一些东西。

最佳答案

在 xml 布局中的按钮上设置 android:onClick="regist"

关于java - onClick 监听器和 onClick on XML 将不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40105757/

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