繁体   English   中英

如何使用登录名(用户名和密码)、按钮更改活动?

[英]How can i change activity using login (username and password) ,button ?

我在 android 中创建了一个登录页面(使用 2 个 EditText 和一个按钮)并保存一些本地定义的用户名和密码。

在执行期间,当用户输入正确的数据时,它会将其活动更改为下一个界面,我可以在其中将项目保留在 listmenu 上。

请帮忙。

使用一个类

public class myDataStore{

public static String uid;
public static String pwd; 
}

其他选项是使用 Android 的应用程序类。 你的问题不清楚,你到底想做什么。

我从您的问题中了解到您想切换活动。

要更改当前活动并启动另一个活动,您需要一个Intent ,一旦您单击提交button调用它。

首先,您需要设置按钮OnClick属性以submit例如,

    <EditText
    android:id="@+id/login"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10" >

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/password"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="textPassword" />

<Button
    android:id="@+id/submit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="submit"
    android:text="Button" />

然后将submit方法添加到您的日志activity

public void submit(View view)
{  
//here you put a condition to check if your login and password are correct
//You can for exemple compare them with values that you have in an sqlite database
if(myLogin.equals("correctLogin") && myPassword.equals("correctPassword"))
    {
        Intent intent = new Intent(yourLogActivity.this, yourListMenuActivity.class);
        startActivity(intent);
    }
}
  String userName = "Coolman"
  String password = "IamTheKing"


  public void attemptLogin(View view)
{  

// Get the text from the editText that the user put in
 String enteredUserName = ((EditText)findViewById(R.id.userNameText)).getText().toString();
 String enteredPassword = ((EditText)findViewById(R.id.passwordText)).getText().toString();

// This will check to see if their login info matches
if(userName.matches(enteredUserName) && password.matches(enteredPassword))
    {
        Intent intent = new Intent(this, yourListMenuActivity.class);
        startActivity(intent);
       // and do what ever else you want to do here
    }

}

将您的 onclick 设置为尝试登录,并且不要使用 == 来检查字符串是否匹配使用方法 .matches()。 希望这可以帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM