我正在创建一个验证登录字段(用户名和密码)的方法。我创建了一个方法并在click事件上调用它,但它无法正常工作。
package com.boyzcorn.android.fyp;
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.Toast;
/** Called when the activity is first created. */
public class login extends Activity {
EditText eText = (EditText)findViewById(R.id.uid);
EditText eText2 = (EditText)findViewById(R.id.editText2);
Button btnSubmit = (Button)findViewById(R.id.sbtn);
Button btnSignup = (Button)findViewById(R.id.signupbtn);
/* I think there is some problem with my method definition but i am not
getting it. */
public void validation(EditText username,EditText pass) {
if (username.getText().toString().equals("") ||
pass.getText().toString().equals("")) {
Toast.makeText(
getApplicationContext(),
"Fill Empty Fields",Toast.LENGTH_SHORT
).show();
} else {
Intent i = new Intent(login.this,order_pushing.class);
startActivity(i);
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
btnSubmit.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
validation(eText,eText2);
}
});
btnSignup.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent i = new Intent(login.this,signup.class);
startActivity(i);
}
});
}
}
答案 0 :(得分:3)
将此代码放在活动{
下EditText eText;
EditText eText2;
Button btnSubmit;
Button btnSignup;
并在 setContentView(R.layout.login);
之后在OnCreate中初始化它eText = (EditText)findViewById(R.id.uid);
eText2 = (EditText)findViewById(R.id.editText2);
btnSubmit = (Button)findViewById(R.id.sbtn);
btnSignup = (Button)findViewById(R.id.signupbtn);
希望这会起作用
答案 1 :(得分:1)
您在设置内容视图之前定义了按钮。在onCreate()
之后将以下行移至setContentView(R.layout.login)
:
EditText eText = (EditText)findViewById(R.id.uid);
EditText eText2 = (EditText)findViewById(R.id.editText2);
Button btnSubmit = (Button)findViewById(R.id.sbtn);
Button btnSignup = (Button)findViewById(R.id.signupbtn);