Android 如何使用登录(用户名和密码)按钮更改活动?
我在android中创建了一个登录页面(使用2个EditText和一个按钮),并保存了一些本地定义的用户名和密码 在执行过程中,当用户输入正确的数据时,它会将其活动更改为下一个界面,在该界面中,我可以在列表菜单上保留项目 请帮助。使用类似Android 如何使用登录(用户名和密码)按钮更改活动?,android,android-activity,Android,Android Activity,我在android中创建了一个登录页面(使用2个EditText和一个按钮),并保存了一些本地定义的用户名和密码 在执行过程中,当用户输入正确的数据时,它会将其活动更改为下一个界面,在该界面中,我可以在列表菜单上保留项目 请帮助。使用类似 public class myDataStore{ public static String uid; public static String pwd; } 另一个选择是使用Android的应用程序类。你的问题不清楚,, 你到底想做什么 我从你的问题中
public class myDataStore{
public static String uid;
public static String pwd;
}
另一个选择是使用Android的应用程序类。你的问题不清楚,,
你到底想做什么 我从你的问题中了解到,你想切换活动 要更改当前活动并启动另一个活动,您需要一个
意图
,一旦单击submit按钮
,就会调用该意图
首先,您需要将按钮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" />
将onclick设置为attemptLogin,不要使用==检查字符串是否匹配。请使用方法.matches()。希望这有帮助 不使用仅使用局部变量的SQL。您的问题不清楚,在输入正确的数据后,您到底想做什么?不要对字符串使用==使用java方法,例如.matches()或.contains()确保您接受一个答案,以获得rep at,并向其他用户显示对您有用的内容
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
}
}