[英]How can i change activity using login (username and password) ,button ?
I created a log in page ( using 2 EditText and one button ) in android and save some local defined username and password .我在 android 中创建了一个登录页面(使用 2 个 EditText 和一个按钮)并保存一些本地定义的用户名和密码。
During execution when user enters the correct data then it changes its activity to next interface where i can keep items on listmenu .在执行期间,当用户输入正确的数据时,它会将其活动更改为下一个界面,我可以在其中将项目保留在 listmenu 上。
Please help .请帮忙。
use a class like使用一个类
public class myDataStore{
public static String uid;
public static String pwd;
}
and other option is using Application Class of Android.其他选项是使用 Android 的应用程序类。 Your question is not clear, What exactly you want to do.
你的问题不清楚,你到底想做什么。
What i understood from your question is that you want to switch activities.我从您的问题中了解到您想切换活动。
To change the current activity and start another one you need an Intent
, that's called once you click on the submit button
.要更改当前活动并启动另一个活动,您需要一个
Intent
,一旦您单击提交button
调用它。
First, you need to set your button OnClick
attribute to submit
for exemple,首先,您需要设置按钮
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" />
Then add a submit
method to your log activity
:然后将
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
}
}
Set your onclick to attemptLogin, and do not use == to check if a string matches use the method .matches().将您的 onclick 设置为尝试登录,并且不要使用 == 来检查字符串是否匹配使用方法 .matches()。 Hope this helps!
希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.