如何从其他类调用并运行函数

发布于 2024-10-07 22:56:38 字数 1670 浏览 5 评论 0原文

我是一名 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

习ぎ惯性依靠 2024-10-14 22:56:38

您不能以这种方式使用“活动”。活动是用户“看到”的东西。将其想象为手机上的屏幕。
总是有一个 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文