zoukankan      html  css  js  c++  java
  • android登录后将用户名显示到主页面(开发日志20)

    例如我这里是手机号作为账号,所以显示的就是手机号

    效果:

    首先是xml布局页面 activity_personal__page.xml

    (我只放了部分的代码,用于实现该功能,其中TextView是用来显示用户名的)

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/person">
    
        <LinearLayout
            android:id="@+id/ziliao"
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:orientation="vertical"
            android:background="#f5efd9"
            android:gravity="center">
    
            <ImageView
                android:id="@+id/img_tx"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:src="@drawable/touxiang"
                android:layout_gravity="center_horizontal"
                />
    
            <TextView
                android:id="@+id/edit_user"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="骑手001"
                android:layout_marginTop="15dp"
                android:textSize="20sp"
                android:textStyle="bold"
                />
        </LinearLayout>
    
    
    </RelativeLayout>

     对应的activity,java文件

    package com.example.express_delivery;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;import android.widget.ImageView;
    import android.widget.TextView;
    
    import com.example.express_delivery.activity.SettingActivity;
    import com.example.express_delivery.login_register.PrefercesService;
    import com.example.express_delivery.order.Person_OrderActivity;import java.util.Map;
    
    public class Personal_PageActivity extends AppCompatActivity {
    
        private TextView edit_user;
        private PrefercesService prefercesService;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_personal__page);
    
            if (getSupportActionBar() != null){
                getSupportActionBar().hide();
            }
    
            prefercesService = new PrefercesService(this);
    
            initView();
    
        }
    
    
        private void initView() {
            edit_user = findViewById(R.id.edit_user);
            Map<String,String> params = prefercesService.getPreferences();
            edit_user.setText(params.get("name"));
    
        }
    
    }

    PrefercesService.java

    package com.example.express_delivery.login_register;
    
    import android.content.Context;
    import android.content.SharedPreferences;
    
    import java.util.HashMap;
    import java.util.Map;
    
    public class PrefercesService {
    
        private Context context;
        public PrefercesService(Context context) {
            super();
            this.context = context;
        }
        /**
         * 保存参数
         * @param name 姓名
         */
        public void save(String name) {
            //第一个参数 指定名称 不需要写后缀名 第二个参数文件的操作模式
            SharedPreferences preferences=context.getSharedPreferences("itcast", Context.MODE_PRIVATE);
            //取到编辑器
            SharedPreferences.Editor editor=preferences.edit();
            editor.putString("name", name);
            //把数据提交给文件中
            editor.commit();
        }
        /**
         * 获取各项配置参数
         * @return
         */
        public Map<String,String> getPreferences(){
            SharedPreferences pre=context.getSharedPreferences("itcast", Context.MODE_PRIVATE);
            //如果得到的name没有值则设置为空 pre.getString("name", "");
            Map<String,String> params=new HashMap<String,String>();
            params.put("name", pre.getString("name", ""));
    
            return params;
    
    
        }
    
    }

    在登录页面LoginActivity.java

    private PrefercesService prefercesService;

    在onCreate函数中:prefercesService = new PrefercesService(this);

    当用户点击登录按钮时,调用函数:

    String name = et_login_phone.getText().toString().trim(); (这里的et_login_phone就是用户输入手机号的控件EditText)

    prefercesService.save(name);

  • 相关阅读:
    Beacon技术是什么?
    exclude kernel or other packages from getting updated
    (OK) running CORE & docker on Fedora 23 server
    (OK) dnf
    (OK) dnf
    rpm
    dnf
    dnf install -y kernel-4.2.3-300.fc23
    paper4—Multi-MPE_Trigger_Algorithm—testing
    paper4—Multi-MPE_Trigger_Algorithm
  • 原文地址:https://www.cnblogs.com/022414ls/p/13544772.html
Copyright © 2011-2022 走看看