用户登录界面

来源:互联网 发布:淘宝评论图片福利 编辑:程序博客网 时间:2024/05/04 03:20

13.

1)圆形图像为一个开源控件---CircleImageView

 

①布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="180dp"
        android:background="?attr/colorPrimary" >
        <de.hdodenhof.circleimageview.CircleImageView
            android:id="@+id/img_head"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="12dp"
            app:civ_border_width="2dp"
            app:civ_border_color="#FFFFFF"
            android:src="@drawable/default_head">
        </de.hdodenhof.circleimageview.CircleImageView>

        <TextView
            android:id="@+id/txt_username"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="50dp"
            android:layout_marginBottom="9dp"
            android:gravity="center"
            android:text="点击登录"
            android:textColor="@color/white"
            android:textSize="16sp" />
    </FrameLayout>


    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="20dp"
            android:orientation="vertical">
            <TextView
                android:id="@+id/txt_my_orders"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="@dimen/largePadding"
                android:background="@drawable/selector_list_item"
                android:drawableLeft="@drawable/icon_list_o"
                android:gravity="center_vertical"
                android:drawablePadding="20dp"
                android:text="我的订单"
                android:textColor="@color/black"/>
            <View style="@style/line_vertical"/>
            <TextView
                android:id="@+id/txt_my_favorite"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="@dimen/largePadding"
                android:background="@drawable/selector_list_item"
                android:drawableLeft="@drawable/icon_favorite"
                android:gravity="center_vertical"
                android:drawablePadding="20dp"
                android:text="我的收藏"
                android:textColor="@color/black"/>
            <View style="@style/line_vertical"/>

            <TextView
                android:id="@+id/txt_my_address"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="@dimen/largePadding"
                android:background="@drawable/selector_list_item"
                android:drawableLeft="@drawable/icon_location"
                android:gravity="center_vertical"
                android:drawablePadding="20dp"
                android:text="我的收货地址"
                android:textColor="@color/black"/>

            <Button
                android:id="@+id/btn_logout"
                android:text="退出登录"
                style="@style/bigRedButton"
                android:layout_marginLeft="15dp"
                android:layout_marginRight="15dp"
                android:layout_marginTop="20dp"
                android:layout_marginBottom="20dp"
                />
        </LinearLayout>
    </ScrollView>
</LinearLayout>

 

②为圆形头像添加点击事件

private void initCircleImage(){
    circleImageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(getContext()LoginActivity.class);
            startActivityForResult(intent0);
        }
    });
}

 

③实现登陆的布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/bg_color">
    
    <com.example.administrator.shopdemo.widget.CNiaoToolBar
        android:id="@id/toolbar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        app:title="登录"
        app:navigationIcon="@drawable/icon_back_32px"
        />
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginTop="40dp"
        android:background="@drawable/selector_view_block"
        android:padding="@dimen/largePadding">

        <com.example.administrator.shopdemo.widget.ClearEditText
            android:id="@+id/etxt_phone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:drawableLeft="@drawable/icon_telphone_32"
            android:drawablePadding="20dp"
            android:hint="请输入手机号码"
            style="@style/editText_base"
            android:inputType="phone"
            />

        <View style="@style/line_vertical"/>
        
        <com.example.administrator.shopdemo.widget.ClearEditText
            android:id="@+id/etxt_pwd"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:drawableLeft="@drawable/icon_lock"
            android:drawablePadding="20dp"
            android:hint="请输入登录密码"
            style="@style/editText_base"
            android:inputType="textPassword"
            />
    </LinearLayout>
    <Button
        android:id="@+id/btn_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/bigRedButton"
        android:text="登 录"
        android:onClick="login"
        android:layout_marginTop="30dp"
        android:layout_margin="20dp"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:padding="@dimen/largePadding"
        android:orientation="horizontal"
        android:gravity="center_vertical">

        <TextView
            android:id="@+id/txt_toReg"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="注册账号"
            android:layout_weight="1"
            android:textColor="@color/blue"
            android:textSize="12sp"
            android:clickable="true"
            android:onClick="register"
            />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="忘记密码"
            android:layout_weight="1"
            android:textColor="@color/blue"
            android:textSize="12sp"
            android:gravity="right"
            android:clickable="true"
            android:onClick="register"
            />
    </LinearLayout>
</LinearLayout>

 

④实现登陆的逻辑

public class LoginActivity extends AppCompatActivity {

    @ViewInject(R.id.etxt_phone)
    private ClearEditText phoneEdit;

    @ViewInject(R.id.etxt_pwd)
    private ClearEditText passwordEdit;

    @ViewInject(R.id.btn_login)
    private Button loginButton;

    @ViewInject(R.id.toolbar)
    private CNiaoToolBar mToolBar;

    private OkHttpHelper okHttpHelper = OkHttpHelper.getInstance();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ViewUtils.inject(this);
        initToolBar();
    }
    private void initToolBar(){
        mToolBar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LoginActivity.this.finish();
            }
        });
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }

    @OnClick(R.id.btn_login)
    public void login(View view){
        String phone = phoneEdit.getText().toString().trim();
        if(TextUtils.isEmpty(phone)){
            ToastUtils.show(this, "请输入手机号码");
            return;
        }
        String pwd = passwordEdit.getText().toString().trim();
        if(TextUtils.isEmpty(pwd)){
            ToastUtils.show(this,"请输入密码");
            return;
        }
        Map<String,String> params = new HashMap<>(2);
        params.put("phone",phone);
        params.put("password"DESUtil.encode(Contants.DES_KEY,pwd));
        okHttpHelper.post(Contants.API.LOGINparams, new SpotsCallBack<LoginRespMsg<User>>(this) {
            @Override
            public void onSuccess(Response responseLoginRespMsg<User> userLoginRespMsg) {
                ShoppingApplication application =  ShoppingApplication.getInstance();
                application.putUser(userLoginRespMsg.getData()userLoginRespMsg.getToken());
                if(application.getIntent() == null){
                    setResult(RESULT_OK);
                    finish();
                }else{
                    application.jumpToTargetActivity(LoginActivity.this);
                    finish();
                }
//                setResult(0);
//                finish();

            }
            @Override
            public void onError(Response response, int codeException e) {

            }
        });
    }

    @OnClick(R.id.txt_toReg)
    public void register(View view){
        Intent intent = new Intent(LoginActivity.this, RegActivity.class);
        startActivity(intent);
    }
}

 

⑤登陆成功后,跳转回页面进行处理

@Override
public void onActivityResult(int requestCode, int resultCodeIntent data) {
    switch (requestCode){
        case 0:
            ToastUtils.show(getContext()"登陆成功");
            User user = ShoppingApplication.getInstance().getUser();
            ToastUtils.show(getContext()(user == null) + "2");
            showUser();
    }
}

 

⑥将布局转换,加载用户头像

private  void showUser(){
    User user = ShoppingApplication.getInstance().getUser();
    ToastUtils.show(getContext()(user == null) + "");
    if(user ==null){
        mbtnLogout.setVisibility(View.GONE);
        mTxtUserName.setText("点击登录");
    }
    else{
        mbtnLogout.setVisibility(View.VISIBLE);
        if(!TextUtils.isEmpty(user.getLogo_url()))
            Picasso.with(getActivity()).load(Uri.parse(user.getLogo_url())).into(circleImageView);
        mTxtUserName.setText(user.getUsername());
    }
}

 

⑦为几个选项添加点击事件,实现其逻辑

@OnClick(R.id.txt_my_orders)
public void toMyOrderActivity(View view){
    startActivity(new Intent(getActivity()MyOrderActivity.class));
}

 

存储User

ShoppingApplication application =  ShoppingApplication.getInstance();
application.putUser(userLoginRespMsg.getData()userLoginRespMsg.getToken());

获取User:

User user = ShoppingApplication.getInstance().getUser();

⑨在ShoppingApplication中有

public User getUser(){
    return user;
}
public void putUser(User userString token){
    this.user = user;
    UserLocalData.putUser(this,user);
    UserLocalData.putToken(this,token);
}

 

实际上是存储在SharedPreferences中:

public class UserLocalData {
    public static void putUser(Context context,User user){
        String user_json =  JSONUtil.toJSON(user);
        PreferencesUtils.putString(contextContants.USER_JSON,user_json);
    }
    public static void putToken(Context context,String token){
        PreferencesUtils.putString(contextContants.TOKEN,token);
    }
    public static User getUser(Context context){
        String user_json= PreferencesUtils.getString(context,Contants.USER_JSON);
        if(!TextUtils.isEmpty(user_json)){
            return  JSONUtil.fromJson(user_json,User.class);
        }
        return  null;
    }
    public static  String getToken(Context context){
        return  PreferencesUtils.getString( context,Contants.TOKEN);
    }
    public static void clearUser(Context context){
        PreferencesUtils.putString(contextContants.USER_JSON,"");
    }
    public static void clearToken(Context context){
        PreferencesUtils.putString(contextContants.TOKEN,"");
    }
}

0 0
原创粉丝点击