简体   繁体   English

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

[英]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.

相关问题 如何使用PHP从MySQL检索的用户名和密码打开另一个活动? - How Can I Open Another Activity based on The Username and Password I Retrieve from MySQL using PHP? 如何使用Volley库在登录应用程序中保存用户名和密码进行发送和请求 - How can I save username and password in login application using volley library to sent and request 登录活动-如果用户忘记密码,如何发送电子邮件给用户? - Login Activity - How can I send email to user if they forgot password? 如何在登录活动模板Android Studio中设置有效的用户名和密码 - How to set valid username and password in login activity template android studio 有人可以告诉我如何将用户名和密码信息从一种意图复制到网站,然后按登录按钮 - can someone tell me how to copy username & password info from one intent to website and press the login button 如何使用用户名/密码通过Devise和Android认证用户? - How can I authenticate a user with Devise and Android using username/password? 在第二个活动中单击一个按钮时,如何使用微调器更改主要活动的背景颜色? - How can I change the background color of a Main Activity by using spinner while clicking a button in the Second Activity? 使用用户名/密码登录Instagram - Login to Instagram using username/password 无法使用Plivo子帐户的用户名和密码端点登录 - Can't Login using Username and Password Endpoint of Plivo Sub Account 如何使用用户名和密码登录Spotify? - How to login to Spotify with username and password?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM