问题
I am doing a project for class where I would like to create a little password login in Android Studio. I want to create something simple and I know how to do it on Java but I don't know how I would go about doing it in this application. I basically want to get up a password box and a button. On down of the button I would like to test the input of the edit text password box to see if it equals the variable. This variable would be set and definite to something like root. I need to find a way to test that output on the password field to see if it equals the variable. If it does then it would move to another page. The code will be below
my Java file:
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";
}
}
my XML file:
<?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>
回答1:
Here getting text from edittest and compare on button click if it matches than go to another activity.
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.
}
}
});
}
}
回答2:
you can get the input from the EditText
and compare it with the password variable in onClick
of the button.
//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
}
}
});
回答3:
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));
}
}
});
回答4:
First, you need to reference your buttons in the java code and then you have to get references to all the necessary textfields too, see the simple implementation below:
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));
}
});
}
}
回答5:
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
}
}
});
回答6:
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));
}
}
});
回答7:
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.
}
}
});
来源:https://stackoverflow.com/questions/45940023/password-text-field