简体   繁体   English

我正在努力使我的应用程序具有“保持登录状态”状态。 我该怎么办?

[英]I'm working on having a “Keep me on Logged in” state on my app. How should i do it?

Here's my xml: 这是我的xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="horizontal"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:background="#FFFFFF">

    <TextView android:id="@+id/welcome_text"
        android:text = "Log-In"   
        android:textColor="#000000"        
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="40dp"
        android:layout_centerHorizontal="true"/>
     <TextView
        android:id="@+id/username_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="20dip"
        android:layout_marginTop="100dip"
        android:layout_marginLeft="30dip"
        android:text="Username:"
        android:textColor="#000000"/>
    <EditText
        android:id="@+id/txt_username"
        android:layout_width="150dip"
        android:layout_height="wrap_content"
        android:background="@android:drawable/editbox_background"
        android:layout_toRightOf="@id/username_text"
        android:layout_alignTop="@id/username_text"/>
     <TextView
        android:id="@+id/password_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/username_text"
        android:layout_marginRight="20dip"
        android:layout_marginTop="30dip"
        android:layout_marginLeft="30dip"
        android:text="Password:"
        android:textColor="#000000"/>
    <EditText
        android:id="@+id/txt_password"
        android:layout_width="150dip"
        android:layout_height="wrap_content"
        android:background="@android:drawable/editbox_background"
        android:layout_toRightOf="@id/password_text"
        android:layout_alignTop="@id/password_text"
        android:layout_below="@id/txt_username"
        android:layout_marginLeft="5dip"
        android:password="true"        />
    <Button
        android:id="@+id/login_button"
        android:layout_width="100dip"
        android:layout_height="wrap_content"
        android:layout_below="@+id/txt_password"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="35dip"
        android:layout_marginLeft="110dip"
        android:text="Submit" />
     <TextView
        android:id="@+id/tv_error"
        android:layout_width="fill_parent"
        android:layout_height="40dip"
        android:textSize="5pt"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/txt_password"
        android:layout_marginRight="9dip"
        android:layout_marginTop="9dip"
        android:layout_marginLeft="15dip"
        android:textColor="#FF0000"
        android:text=""/>
     <TextView android:id="@+id/tv_login"
        android:text = "@string/Login"   
        android:textColor="#000000"        
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="40dp"
        android:layout_below="@id/login_button"
        android:layout_centerHorizontal="true"/>
     <RadioGroup
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@id/tv_login"
      android:orientation="horizontal"
      android:layout_centerHorizontal="true">
      <RadioButton android:id="@+id/on"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginTop="9dip"
          android:text="On" 
          android:textColor="#000000"/>
      <RadioButton android:id="@+id/off"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginTop="9dip"
          android:layout_marginLeft="20dip"
          android:text="Off" 
          android:textColor="#000000"/>
    </RadioGroup>
</RelativeLayout>

and here's my java: 这是我的java:

public class LogInActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.loginactivity);

        final RadioButton radio_on = (RadioButton) findViewById(R.id.on);
        final RadioButton radio_off = (RadioButton) findViewById(R.id.off);

        Button launch = (Button)findViewById(R.id.login_button);
        launch.setOnClickListener(new OnClickListener()
        {
         public void onClick(View v) {
           EditText usernameEditText = (EditText) findViewById(R.id.txt_username);
           EditText passwordEditText = (EditText) findViewById(R.id.txt_password);
           TextView loginTextView = (TextView)findViewById(R.id.tv_error);

           String sUserName = usernameEditText.getText().toString();
           String sPassword = passwordEditText.getText().toString();

            if(sUserName.equals("numlock") && sPassword.equals("numlock")) {
                Intent intent = new Intent(LogInActivity.this, HomeActivity.class);
                startActivity(intent);
            }
             else {
                loginTextView.setText("Login failed. Username and/or password doesn't match.");
                 }
            }
        }); 
    }
}

I want to assign the keep me logged in state on the radio buttons. 我想在单选按钮上分配“保持登录状态”。 Any help? 有什么帮助吗?

您应该将登录凭据保存在sharedPreferences或Sqlite中,并在登录活动时检查是否找到任何保存的数据,然后使用保存的数据登录,否则向用户询问凭据。

您可以使用SharedPreference静态变量Application类来完成此操作,这将有助于您保持User的状态

暂无
暂无

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

相关问题 我想在我的应用程序上进行 m3u8 直播。 那么任何人都可以帮助我如何做到这一点? 我尝试使用 exoplayer 但我失败了 - i want to do m3u8 live streaming on my app. So can anyone help me how can i do this ? i tried with exoplayer but i failed 如何让用户登录我的网站数月? - How do I keep a user logged into my site for months? 我想通过在当前应用程序中按一个按钮来终止/关闭特定应用程序。 我怎样才能做到这一点? - I want to kill/ close a particular app by pressing a button in my current app. How can I do this? 我正在尝试制作社交媒体应用。 如何将我的应用程序连接到mySql和Xampp? - I am trying to make a social media app. How do I connect my app to a mySql and, Xampp? 我正在开发一个基本的toDo应用。 当我长按该项目时,应将其从列表中删除,但不会执行任何操作。 我究竟做错了什么? - I am working on a basic toDo app. When I long press on the item it should be deleted from my list but it does nothing. What am i doing wrong? 我在使用 Firebase 创建注册时出错 我的应用程序实际上正在运行,但是当我按下注册按钮时它一直显示失败 - I'm having an error creating an registration using Firebase My app is actually running but when I pressed on register button It keep on showing fail 对于我的第一个简单Web应用程序的身份验证,我不知所措。 我正在使用JSF2.0 + Tomcat - I'm overwhelmed with authenticating users for my first simple web app. I'm using JSF2.0 + Tomcat 我想从我的Java Web应用程序访问他们自己的保管箱的用户。 我怎样才能做到这一点? - I want to give access to users of their own dropbox from my java web app. How can I do that? 无论手机状态如何,我如何确保我的应用程序继续工作? - How do I ensure that my app keeps working regardless of phone state? 我的JButton应该执行,我应该在我的窗格上看到一个矩形。 我如何让它可见? - My JButton should execute and a rectangle should me seen on my Pane. How do i make it visible?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM