- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我在尝试使用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/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!