2017-08-29 98 views
-1

我正在做一個類的項目,我想在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> 

回答

1

這裏從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. 
        } 
       } 
      }); 
     } 
    } 
+0

嘿, 是否有特定的軟件包,我需要這個?它出現了一些錯誤,但是迄今爲止我所見過的最好的答案。 謝謝 –

+0

不,你不需要任何外部軟件包。它會自動獲取所需的包或TextView或Button等。它顯示哪個錯誤?你可以分享嗎? –

+0

是的,你只需要導入一些文件。對於這只是去紅字,並按住ALT鍵,然後按回車比它顯示導入比擊中輸入。對所有單詞都這樣做.. –

0

可以從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 
        } 
       } 
      }); 
0

首先,您需要引用在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)); 
            } 
      }); 
     } 
    } 
0
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)); 
      } 
     } 
    }); 
0
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 
      } 
     } 
    }); 
0
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)); 
     } 
} 
}); 
0
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. 
        } 
       } 
      });