2012-05-08 128 views
0
EditText txtUserName; 
EditText txtPassword; 
Button btnLogin; 
Button btnCancel; 

    /** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 


    txtUserName=(EditText)this.findViewById(R.id.txtUname); 
    txtPassword=(EditText)this.findViewById(R.id.txtPwd); 
    btnLogin=(Button)this.findViewById(R.id.btnLogin); 
     btnLogin.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) {    
        Intent myIntent = new Intent(v.getContext(), AddName.class); 
        startActivityForResult(myIntent, 0); 
       if((txtUserName.getText().toString()).equals(txtPassword.getText().toString())){ 
        Toast.makeText(LoginappActivity.this, "Login Successful",Toast.LENGTH_LONG).show(); 
        } else{ 
        Toast.makeText(LoginappActivity.this, "Invalid Login",Toast.LENGTH_LONG).show(); 
        } 
         } 
          }); 

}   
     } 

我爲我的應用創建了一個登錄頁面。而我在我的模擬器中運行,如果用戶名和密碼相匹配,應該進入下一個畫面,但在運行此,如果用戶名和密碼是錯誤的應用程序將會第二頁任何人都可以提出在android中創建登錄頁面

回答

1
package com.app.NewProjectHttpDemo; 

import java.io.InputStream; 
import java.net.URL; 
import java.util.ArrayList; 

import android.content.Context; 
import android.graphics.drawable.Drawable; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.ImageView; 
import android.widget.TextView; 

EditText txtUserName; 
EditText txtPassword; 
Button btnLogin; 
Button btnCancel; 

    /** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 


    txtUserName=(EditText)this.findViewById(R.id.txtUname); 
    txtPassword=(EditText)this.findViewById(R.id.txtPwd); 
    btnLogin=(Button)this.findViewById(R.id.btnLogin); 
     btnLogin.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) {    

       if((txtUserName.getText().toString()).equals(txtPassword.getText().toString())){ 
Intent myIntent = new Intent(v.getContext(), AddName.class); 
        startActivityForResult(myIntent, 1); 
        Toast.makeText(LoginappActivity.this, "Login Successful",Toast.LENGTH_LONG).show(); 
        } else{ 
        Toast.makeText(LoginappActivity.this, "Invalid Login",Toast.LENGTH_LONG).show(); 
        } 
         } 
          }); 

}   
     } 

替換此代碼與您的代碼,這將很好地工作

0
EditText txtUserName; 
EditText txtPassword; 
Button btnLogin; 
Button btnCancel; 

    /** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 


    txtUserName=(EditText)this.findViewById(R.id.txtUname); 
    txtPassword=(EditText)this.findViewById(R.id.txtPwd); 
    btnLogin=(Button)this.findViewById(R.id.btnLogin); 

    btnLogin.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) {    

      if((txtUserName.getText().toString()).equals(txtPassword.getText().toString())){ 
       Intent firstIntent = new Intent(v.getContext(), SecondActivity.class); 
       startActivity(firstIntent); 

       } else{ 
         Intent secondtIntent = new Intent(v.getContext(),SecondActivity.class); 
       startActivity(secondIntent); 
       } 
        } 
         }); 

}
}

0
  btnLogin=(Button)this.findViewById(R.id.btnLogin); 
     btnLogin.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) {         
       if((txtUserName.getText().toString()).equals(txtPassword.getText().toString())){ 
        Toast.makeText(LoginappActivity.this, "Login Successful",Toast.LENGTH_LONG).show(); 

        Intent myIntent = new Intent(v.getContext(), AddName.class); 
        startActivity(myIntent); 

        } else{ 
        Toast.makeText(LoginappActivity.this, "Invalid Login",Toast.LENGTH_LONG).show(); 
        } 
        } 
       });