2013-06-20 36 views
0

我正在應用程序的登錄頁面上工作。我想比較硬編碼值與用戶名,密碼作爲輸入。代碼 線目前使用的是:在登錄頁面中,將硬編碼值與用戶名,密碼作爲輸入進行比較

usr = (EditText) findViewById(R.id.usr); 
     pwd = (EditText) findViewById(R.id.pwd);  
    error = (TextView) findViewById(R.id.tv_error);  
    String n1="i077653" ; 
     if(n1.equals(usr.getText().toString()) && pwd.equals("1234"))  
    {   error.setText("login success");  }   
    else  {  error.setText("LOGIN failed");   } 
. 

但它不工作。可你提出了一個解決方案?

完整代碼: XML文件:

XML file :<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    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=".MainActivity" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:background="#FF6633" 
     android:paddingLeft="60dp" 
     android:paddingRight="100dp" 
     android:text="@string/SA" /> 

    <TextView 
     android:id="@+id/tv_pw" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/pwd" 
     android:layout_alignBottom="@+id/pwd" 
     android:layout_alignLeft="@+id/tv_un" 
     android:text="Password:" 
     android:textColor="#444444" 
     android:textSize="7pt" /> 

    <TextView 
     android:id="@+id/tv_un" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/usr" 
     android:layout_alignBottom="@+id/usr" 
     android:layout_alignParentLeft="true" 
     android:text="User Name:" 
     android:textColor="#444444" 
     android:textSize="7pt" /> 

    <EditText 
     android:id="@+id/usr" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/btn_login" 
     android:layout_below="@+id/textView1" 
     android:layout_marginTop="20dp" 
     android:ems="7" 
     android:hint="@string/User" > 

     <requestFocus /> 
    </EditText> 

    <EditText 
     android:id="@+id/pwd" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/usr" 
     android:layout_below="@+id/usr" 
     android:layout_marginTop="18dp" 
     android:ems="7" 
     android:hint="@string/Pwd" 
     android:inputType="textPassword" /> 

    <Button 
     android:id="@+id/btn_login" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/pwd" 
     android:layout_marginTop="18dp" 
     android:layout_toRightOf="@+id/tv_un" 

     android:text="@string/Button" /> 
<TextView 
     android:id="@+id/tv_error" 
     android:layout_width="fill_parent" 
     android:layout_height="40dip" 
     android:textSize="7pt" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@id/btn_login" 
     android:layout_marginRight="9dip" 
     android:layout_marginTop="15dip" 
     android:layout_marginLeft="15dip" 
     android:textColor="#AA0000" 
     android:text=""/> 



    </RelativeLayout> 

的Java文件:

package com.example.survey; 

import android.os.Bundle; 

import android.app.Activity; 
import android.text.Editable; 
import android.view.Menu; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 


public class MainActivity extends Activity { 
    EditText usr ; 
    EditText pwd ; 
    Button log; 
    TextView error; 


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


    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    public void check(View view){ 
    usr = (EditText) findViewById(R.id.usr); 
    pwd = (EditText) findViewById(R.id.pwd); 
    error = (TextView) findViewById(R.id.tv_error); 
    String n1="i077653" ; 


    if(n1.equalsIgnoreCase(usr.getText().toString()) && pwd.equalsIgnoreCase("1234"))  
    {   error.setText("login success");  }   
    else  {  error.setText("LOGIN failed");   } 


    } 
    } 
+0

郵政完整的OnCreate代碼... – amalBit

+0

在這個時間碼現在檢查我的答案。 – amalBit

回答

0
String n1="i077653" ; 
    if(n1.equals(usr.getText().toString()) && pwd.equals("1234"))  
{   error.setText("login success");  }   
else  {  error.setText("LOGIN failed");   } 

嘗試像onclick某些事件或其他一些事件

+0

好吧,大家所有的答案都不起作用,我已經試過了。你有另一種方法或代碼,我可以使用? –

+0

你試過了什麼? – dhams

+0

把它放在onClickListener –

0

嘗試用

if(n1.equalsIgnoreCase(usr.getText().toString()) && pwd.equalsIgnoreCase("1234"))  
    {   error.setText("login success");  }   
    else  {  error.setText("LOGIN failed");   } 

而且是塊日誌只有當用戶= i077653和密碼= 1234 如果你想要「如果用戶= i077653或密碼= 1234」更改& & with ||

+0

好吧,大家所有的答案都不起作用,我已經試過了。你有另一種方法或代碼,我可以使用? –

1

你的代碼更改爲:

if(n1.equals(usr.getText().toString()) && (pwd.getText().toString()).equals("1234"))  

這裏PWD是編輯文本變量。它包含的不是輸入的值。

編輯:()

公共類MainActivity延伸活動{ 的EditText USR; EditText pwd; 按鈕日誌; TextView錯誤;

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


    usr = (EditText) findViewById(R.id.usr); 
    pwd = (EditText) findViewById(R.id.pwd); 
    error = (TextView) findViewById(R.id.tv_error); 
    log=(Button) findViewById(R.id.tv_log); 
    String n1="i077653" ; 

    log.setOnClickListener(new OnClickListener(){ 

     @Override 
     public void onClick(View v) { 

      if(n1.equalsIgnoreCase(usr.getText().toString()) &&(pwd.getText().toString()).equalsIgnoreCase("1234")) 
    { 

    error.setText("login success");  

    }   
    else{ 

    error.setText("LOGIN failed"); 

    } 
    } 

    }); 

    }//end of oncreate 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 
    } 

建議:請遵循此tutorial

+0

如果有幫助,請選擇答案... – amalBit

+0

好吧,大家好,你們所有的答案都無法解決,我已經嘗試過了。你有另一種方法或代碼,我可以使用? –

0
if(n1.equals(usr.getText().toString()) && "1234".equals(pwd.getText().toString())){ 

    // login success 
}else{ 
... 
} 
+0

好吧,你們所有的答案都不起作用,我已經嘗試過了。你有另一種方法或代碼,我可以使用? –

相關問題