android+简单的登录界面+OKhttp请求

builder gradle中加:

compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.squareup.okio:okio:1.5.0'
    compile 'com.squareup.okhttp3:okhttp:3.2.0'
    compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
    compile 'com.google.code.gson:gson:2.8.2'
    compile 'com.android.support:recyclerview-v7:24.0.0'
    compile 'com.github.bumptech.glide:glide:3.7.0'
    testCompile 'junit:junit:4.12'

res布局:

activity_login


    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.bwie.dierzhoukao.LoginActivity"
    android:weightSum="1">

            android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="个人信息"
        android:gravity="center"
 />

            android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="#000000"/>

            android:layout_width="match_parent"
        android:layout_height="70dp"
        >

                    android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="头像"
            android:layout_centerVertical="true"
            />
                    android:id="@+id/user_touxiang"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:src="@mipmap/ic_launcher"
            />
   
            android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="#000000"/>
            android:layout_width="match_parent"
        android:layout_height="70dp"
        >

                    android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="用户名"
            android:layout_centerVertical="true"
            />
                    android:id="@+id/user_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:text="username字段"
            />
   
            android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="#000000"/>
            android:layout_width="match_parent"
        android:layout_height="70dp"
        >

                    android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="昵称"
            android:layout_centerVertical="true"
            />
                    android:id="@+id/user_nicheng"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:text="逆流成河~"
            />
   
            android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="#000000"/>

            android:layout_width="match_parent"
        android:layout_height="match_parent">
            android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="退出登录"
        android:onClick="tuichu"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="61dp"
        android:layout_marginStart="61dp"
        android:layout_marginTop="128dp"
        android:id="@+id/button" />
                    android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="跳转到商品搜索页面"
            android:onClick="tiaozhuan"
            android:layout_alignBaseline="@+id/button"
            android:layout_alignBottom="@+id/button"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_marginRight="27dp"
            android:layout_marginEnd="27dp" />
   


activity_main

    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.bwie.dierzhoukao.MainActivity">

            android:id="@+id/et_phone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:hint="请输入手机号" />

            android:id="@+id/et_pass"
        android:hint="请输入密码"
        android:layout_marginTop="50dp"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

            android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

                    android:id="@+id/login"
            android:onClick="login"
            android:text="登录"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="74dp"
            android:layout_marginStart="74dp"
            android:layout_marginTop="96dp"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />
                    android:id="@+id/zhuce"
            android:onClick="zhuce"
            android:text="注册"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/login"
            android:layout_alignBottom="@+id/login"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_marginRight="81dp"
            android:layout_marginEnd="81dp" />
   



activity_xiangqing


    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.bwie.dierzhoukao.XiangqingActivity">
            android:id="@+id/recycleview"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

   

activity_zhuce

    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.bwie.dierzhoukao.ZhuceActivity">
            android:id="@+id/et_phone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:hint="请输入手机号" />

            android:id="@+id/et_pass"
        android:hint="请输入密码"
        android:layout_marginTop="50dp"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

            android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

            android:id="@+id/zhucechenggong"
        android:onClick="zhucechenggong"
        android:text="注册"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="98dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="137dp"
        android:layout_marginEnd="137dp" />
   


item


    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >
            android:id="@+id/good_img"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@mipmap/ic_launcher"
        />
            android:id="@+id/good_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity=""
        android:text="fsdfasdfasd"
        />




MyAdapter

package com.bwie.dierzhoukao.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bwie.dierzhoukao.R;

import java.util.List;

/**
 * Created by TA on 2017/10/14.
 */

public class MyAdapter extends  RecyclerView.Adapter{
    private Context context;
    private List namelist ;
    private List imglist ;
    private View view;

    public MyAdapter(Context context, List namelist, List imglist) {
        this.context = context;
        this.namelist = namelist;
        this.imglist = imglist;
    }
    //创建ViewHolder
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        view = LayoutInflater.from(context).inflate(R.layout.item, parent,
                false);
        MyViewHolder viewHolder = new MyViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
            holder.name.setText(namelist.get(position));
            Glide.with(context).load(imglist.get(position)).into(holder.img);
    }

    @Override
    public int getItemCount() {
        return namelist.size() ;
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        TextView name;
        ImageView img;

        public MyViewHolder(View itemView) {
            super(itemView);

            name = itemView.findViewById(R.id.good_name);
            img = itemView.findViewById(R.id.good_img);

        }
    }
}
Bean类


导入util包

LoginActivity

package com.bwie.dierzhoukao;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import java.nio.charset.MalformedInputException;

public class LoginActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

    }
    public void tuichu(View v){
        Intent intent = new Intent(LoginActivity.this , MainActivity.class) ;
        startActivity(intent);
    }
    public void tiaozhuan(View v){
        Intent intent = new Intent(LoginActivity.this , XiangqingActivity.class) ;
        startActivity(intent);
    }
}

MainActivity

package com.bwie.dierzhoukao;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void login(View v){
        Intent intent = new Intent(MainActivity.this , LoginActivity.class) ;
        startActivity(intent);
    }
    public void zhuce(View v){
        Intent intent = new Intent(MainActivity.this , ZhuceActivity.class) ;
        startActivity(intent);
    }
}

MyApp

package com.bwie.dierzhoukao;

import android.app.Application;
import android.content.Context;

/**
 * Created by TA on 2017/10/14.
 */

public class MyApp extends Application{
    private static Context instance;

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
    }

    public static Context getInstance() {
        return instance;
    }

}

XiangqingActivity

package com.bwie.dierzhoukao;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.bwie.dierzhoukao.adapter.MyAdapter;
import com.bwie.dierzhoukao.bean.Bean;
import com.bwie.dierzhoukao.util.GsonArrayCallback;
import com.bwie.dierzhoukao.util.GsonObjectCallback;
import com.bwie.dierzhoukao.util.OkHttp3Utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

public class XiangqingActivity extends AppCompatActivity {

    String path ="http://result.eolinker.com/umIPmfS6c83237d9c70c7c9510c9b0f97171a308d13b611?uri=homepage";
    List namelist = new ArrayList<>() ;
    List imglist = new ArrayList<>() ;
    private RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xiangqing);
        initView();
        getData();
    }

    private void initView() {
        recyclerView = (RecyclerView) findViewById(R.id.recycleview);
    }

    private void getData() {
        OkHttp3Utils.doGet(path, new GsonObjectCallback() {
            @Override
            public void onUi(Bean bean) {
                for (int j = 0 ; j < bean.getData().getSubjects().size(); j++){
                    for (int i = 0; i < bean.getData().getSubjects().get(j).getGoodsList().size(); i++) {
                        String name = bean.getData().getSubjects().get(j).getGoodsList().get(i).getGoods_name();
                        String url = bean.getData().getSubjects().get(j).getGoodsList().get(i).getGoods_img();
                        namelist.add(name);
                        imglist.add(url);
                    }
                }



                MyAdapter myAdapter = new MyAdapter(XiangqingActivity.this, namelist , imglist);
                recyclerView.setAdapter(myAdapter);//设置适配器
                recyclerView.setLayoutManager(new GridLayoutManager(XiangqingActivity.this, 3));
            }

            @Override
            public void onFailed(Call call, IOException e) {

            }
        });
    }
}
ZhuceActivity

package com.bwie.dierzhoukao;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import com.bwie.dierzhoukao.util.GsonObjectCallback;
import com.bwie.dierzhoukao.util.OkHttp3Utils;

public class ZhuceActivity extends AppCompatActivity {

    String path = "http://120.27.23.105/user/reg";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhuce);
        getData();
    }

    private void getData() {
        OkHttp3Utils.doGet(path, new GsonObjectCallback<>() {
        });
    }

    public void zhucechenggong(View v){
        Intent intent = new Intent(ZhuceActivity.this , MainActivity.class) ;
        startActivity(intent);
    }
}
清单文件


    package="com.bwie.dierzhoukao">
   
   
   
   
   

        android:name=".MyApp"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
       
           
               

               
           

       

       
       
       
   


你可能感兴趣的:(android)