gpt4 book ai didi

android - 定义的方法永远不会被调用

转载 作者:太空宇宙 更新时间:2023-11-03 13:34:56 25 4
gpt4 key购买 nike

我正在创建一种方法来验证我的登录字段(用户名和密码)。我创建了一个方法并在点击事件时调用它,但它不起作用。

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);
}
});

}
}

最佳答案

将此代码放在 Activity 下{

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);

希望这能奏效

关于android - 定义的方法永远不会被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8414225/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com