密码文本字段
问题描述:
我正在做一个类的项目,我想在Android Studio中创建一个小密码登录。我想创建一些简单的东西,我知道如何在Java上完成它,但我不知道如何在此应用程序中执行此操作。我基本上想要起来一个密码框和一个按钮。在该按钮的下方,我想测试编辑文本密码框的输入以查看它是否等于变量。这个变量将被设置和确定为像根。我需要找到一种方法来测试密码字段上的输出以查看它是否等于变量。如果是的话,它会移动到另一个页面。该代码将低于密码文本字段
我的Java文件:
package com.example.murdocbgould.passwordpt4;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String passwordA = "root";
}
}
我的XML文件:
<?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.murdocbgould.passwordpt4.MainActivity">
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword"
tools:layout_editor_absoluteX="85dp"
tools:layout_editor_absoluteY="260dp" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password:"
tools:layout_editor_absoluteX="160dp"
tools:layout_editor_absoluteY="226dp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
tools:layout_editor_absoluteX="148dp"
tools:layout_editor_absoluteY="437dp" />
<TextView
android:id="@+id/textView2"
android:layout_width="328dp"
android:layout_height="43dp"
android:text="Bluetooth Texting Login"
android:textSize="30sp"
tools:layout_editor_absoluteX="28dp"
tools:layout_editor_absoluteY="147dp" />
</android.support.constraint.ConstraintLayout>
答
这里从edittest获取文本,并在按钮点击时比较匹配性,而不是转到其他活动。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final String passwordA = "root";
EditText editText = (EditText) findViewById(R.id.editText);
Button button = (Button) findViewById(R.id.button);
TextView textView2 = (TextView) findViewById(R.id.textView2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
textView2.setText(editText.getText().toString().trim());
if(editText.getText().toString().trim().equals(passwordA)){
Intent i = new Intent(MainActivity.this, SecondActivity.class);
startActivity(i);
}else{
// Do what you want when password is not matches.
}
}
});
}
}
答
可以从EditText
获取输入,并将其与密码变量的onClick
比较按钮。
//Making reference of edittext.
EditText etPassword = (EditText)findViewById(R.id.editText)
// setting onclick to Button.
((Button)findViewById(R.id.button)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// checking the required condition.
if(etPassword.getText().toString().trim().equal(YOUR_PASSWORD_VARIABLE))
{
//PASSWORD MATCHES
}else
{
//PASSWORD MISSMATCHE
}
}
});
答
首先,您需要引用在Java代码中的按钮,然后你必须得到引用到所有必要的文本框太多,请参见下面的简单实现:
package com.example.murdocbgould.passwordpt4;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//make references to your edittext and buttons in the activity_main.xml
EditText passwordField = (EditText) findViewById(R.id.editText);
Button submitButton = (Button) findViewById(R.id.button);
String passwordA = "root";
//listen for button clicks here
submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String password = passwordField.getText().toString();
//compare your password with password and continue
//if you wish to move to another page (which I assume is an activity), edit the next line
startActivity(new Intent(MainActivity.this, NewActivity.class));
}
});
}
}
答
EditText editText = (EditText)findViewById(R.id.edittext);
Button button = (Button)findViewById(R.id.Button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (editText.getText().toString().equals(passwordA)){
startActivity(new Intent(this,OtherActivity.class));
}
}
});
答
EditText editText = (EditText)findViewById(R.id.edittext);
Button button = (Button)findViewById(R.id.Button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (editText.getText().toString().equals(passwordA)){
startActivity(new Intent(this,OtherActivity.class));
}else{
//Pasword Not Match
}
}
});
答
EditText mEditText = (EditText)findViewById(R.id.edittext);
Button mBtn = (Button)findViewById(R.id.Button);
mBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (editText.getText().toString().equals(passwordA)){
startActivity(new Intent(this,AnotherActivity.class));
}
}
});
答
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
textView.setText(editText.getText().toString().trim());
if(editText.getText().toString().trim().equals(passwordA)){
Intent i = new Intent(MainActivity.this, SecondActivity.class);
startActivity(i);
}else{
// Show the user as a Toast that the password is incorrect.
}
}
});
嘿, 是否有特定的软件包,我需要这个?它出现了一些错误,但是迄今为止我所见过的最好的答案。 谢谢 –
不,你不需要任何外部软件包。它会自动获取所需的包或TextView或Button等。它显示哪个错误?你可以分享吗? –
是的,你只需要导入一些文件。对于这只是去红字,并按住ALT键,然后按回车比它显示导入比击中输入。对所有单词都这样做.. –