如何从其他类调用并运行函数
我是一名 Android 开发新手。我目前在课堂上遇到问题。我使用Login.java来分离功能。下面是我的代码:
public class GTSMobile extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
Login lg = new Login();
lg.Chk_Login();
}
}
public class Login extends Activity{
public void Chk_Login() {
Button launch = (Button)findViewById(R.id.login_button);
launch.setOnClickListener( new OnClickListener()
{ @Override
public void onClick(View viewParam)
{
EditText usernameEditText = (EditText) findViewById(R.id.txt_username);
EditText passwordEditText = (EditText) findViewById(R.id.txt_password);
String sUserName = usernameEditText.getText().toString();
String sPassword = passwordEditText.getText().toString();
if(sUserName.length() == 0 || sPassword.length() == 0){
ShowOKAlert();
}else{
setContentView(R.layout.main);
}
}
}); // end of launch.setOnclickListener
}
void ShowOKAlert(){
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Login Fail");
alertDialog.setMessage("Please Enter UserName and Password");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.setIcon(R.drawable.icon);
alertDialog.show();
}
}
I am beginning Android developer. I am currently experiencing a problem with my class. I used Login.java to separate function. Below is my code:
public class GTSMobile extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
Login lg = new Login();
lg.Chk_Login();
}
}
public class Login extends Activity{
public void Chk_Login() {
Button launch = (Button)findViewById(R.id.login_button);
launch.setOnClickListener( new OnClickListener()
{ @Override
public void onClick(View viewParam)
{
EditText usernameEditText = (EditText) findViewById(R.id.txt_username);
EditText passwordEditText = (EditText) findViewById(R.id.txt_password);
String sUserName = usernameEditText.getText().toString();
String sPassword = passwordEditText.getText().toString();
if(sUserName.length() == 0 || sPassword.length() == 0){
ShowOKAlert();
}else{
setContentView(R.layout.main);
}
}
}); // end of launch.setOnclickListener
}
void ShowOKAlert(){
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Login Fail");
alertDialog.setMessage("Please Enter UserName and Password");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.setIcon(R.drawable.icon);
alertDialog.show();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能以这种方式使用“活动”。活动是用户“看到”的东西。将其想象为手机上的屏幕。
总是有一个 Activity 被显示给用户&你不能只在它们之间进行方法调用。
查看此内容以获取有关 Activity 生命周期的更多信息:
http://developer.android.com/reference/android/app/Activity.html
您可以使用以下命令启动一个新的 Activity:
startActivity(..),不使用“new YourActivity(..)”
但是,在您的代码中,我看不到您为什么要启动新活动的原因。只需在您的第一个 Activity 中编写一个方法 checkLogin(...) 即可。
希望对您有帮助。
干杯
You cannot work with Activities this way. An Activity is something the user "sees". Imagine it as being a screen on the phone.
There's always one Activity being shown to the user & you can't just do method calls between them.
Check out this for more information about the Activity lifecycle:
http://developer.android.com/reference/android/app/Activity.html
You would start a new Activity using:
startActivity(..), not using "new YourActivity(..)"
However, in your code, I see no reason why you would start a new Activity. Just write a method checkLogin(...) in your first Activity.
Hope that helps you.
Cheers