With lots of help, I have gotten this android class activity down to only one error. Eclipse ask me to add ")" ,"}", and ";" why though?
package com.android.drinksonme;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Screen2 extends Activity {
// Declare our Views, so we can access them later
private EditText etUsername;
private EditText etPassword;
private Button btnLogin;
private Button btnSignUp;
private TextView lblResult;
@Override
publi开发者_运维知识库c void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Get the EditText and Button References
etUsername = (EditText)findViewById(R.id.username);
etPassword = (EditText)findViewById(R.id.password);
btnLogin = (Button)findViewById(R.id.login_button);
btnSignUp = (Button)findViewById(R.id.signup_button);
lblResult = (TextView)findViewById(R.id.result);
btnLogin.setOnClickListener(new OnClickListener () {
public void onClick(View v){
final String username = etUsername.getText().toString();
final String password = etPassword.getText().toString();
if(username.equals("test") && password.equals("test")){
Intent i = new Intent(Screen2.this, DrinksTwitter.class);
startActivity(i);}
else
lblResult.setText("Invalid username or password."); }
/* final Intent k = new Intent(Screen2.this, SignUp.class);
btnSignUp.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
startActivity(k);
}*/
}
);
}
}
One compile error is meaningless. If you fix that error, it could be hiding hundreds.
But you're missing a curly brace. Go through and make sure all your curly braces are matched correctly.
Edit:
To your updated code, now your
String username = etUsername.getText().toString();
String password = etPassword.getText().toString();
are being defined when you create the click listener. ie before oncreate is finished and before the user can possibly enter anything.
You have:
(new OnClickListener () {
if( blah blah
It must be something like:
(new OnClickListener () {
public void onClick(View v){
if( blah blah
}
});
With regards to your new error... what about this:
btnLogin.setOnClickListener(new OnClickListener () {
public void onClick(View v){
String username = etUsername.getText().toString();
String password = etPassword.getText().toString();
if(username.equals("test") && password.equals("test")){
Intent i = new Intent(Screen2.this, DrinksTwitter.class);
startActivity(i);
}
else
lblResult.setText("Invalid username or password.");
}
}
);
This seems to work for me:
public class LoginActivity extends Activity {
private EditText UserName;
private EditText Password;
private Button login;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
login = (Button)findViewById(R.id.login);
UserName = (EditText)findViewById(R.id.username);
Password = (EditText)findViewById(R.id.password);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String uName = UserName.getText().toString();
String pWord = Password.getText().toString();
if(uName.equals("test") && pWord.equals("test"))
{
Toast.makeText(getBaseContext(), "Yup", Toast.LENGTH_SHORT).show();
}
}
}
);
}
}
精彩评论