1. 程式人生 > >有關登陸註冊以及二維碼+輪播圖,使用mvp自己做的

有關登陸註冊以及二維碼+輪播圖,使用mvp自己做的

//依賴吧

    implementation 'com.squareup.okhttp3:okhttp:3.6.0'
    implementation 'com.squareup.okio:okio:1.11.0'
    implementation 'cn.yipianfengye.android:zxing-library:2.2'
    implementation 'com.google.code.gson:gson:2.8.5'
    implementation 'com.squareup.picasso:picasso:2.3.2'

 

許可權吧

 <uses-permission android:name="android.permission.INTERNET" />
 <uses-permission android:name="android.permission.CAMERA" />

 <uses-feature android:name="android.hardware.camera" /> <!-- 使用照相機許可權 -->
 <uses-feature android:name="android.hardware.camera.autofocus" />

 

//首先看一下輪播圖的介面卡

 

public class BannerAdapter extends PagerAdapter{


    private Context context;
    private List<BannerBean.DataBean> data;

    public BannerAdapter(Context context, List<BannerBean.DataBean> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {

        ImageView imageView = new ImageView(context);
        imageView.setScaleType(imageView.getScaleType().FIT_XY);

        BannerBean.DataBean dataBean = data.get(position % data.size());

        String icon = dataBean.getIcon();

        Picasso.with(context).load(icon).into(imageView);
        container.addView(imageView);


        return imageView;

    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }
}

 

//首先看一下輪播圖的介面卡

//介面和bean

public class BannerBean {


    /**
     * msg :
     * code : 0
     * data : [{"aid":1,"createtime":"2017-12-26T21:49:44","icon":"https://www.zhaoapi.cn/images/quarter/ad1.png","productId":null,"title":"第十三界瑞麗模特大賽","type":0,"url":"http://m.mv14449315.icoc.bz/index.jsp"},{"aid":2,"createtime":"2017-12-26T21:49:44","icon":"https://www.zhaoapi.cn/images/quarter/ad2.png","productId":null,"title":"文化藝術節","type":0,"url":"http://m.mv14449315.icoc.bz/index.jsp"},{"aid":3,"createtime":"2017-12-26T21:49:44","icon":"https://www.zhaoapi.cn/images/quarter/ad3.png","productId":null,"title":"直播封面標準","type":0,"url":"http://m.mv14449315.icoc.bz/index.jsp"},{"aid":4,"createtime":"2017-12-26T21:49:44","icon":"https://www.zhaoapi.cn/images/quarter/ad4.png","productId":"1","title":"人氣誰最高,金主誰最豪氣","type":1,"url":""}]
     */

    private String msg;
    private String code;
    private List<DataBean> data;

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public List<DataBean> getData() {
        return data;
    }

    public void setData(List<DataBean> data) {
        this.data = data;
    }

    public static class DataBean {
        /**
         * aid : 1
         * createtime : 2017-12-26T21:49:44
         * icon : https://www.zhaoapi.cn/images/quarter/ad1.png
         * productId : null
         * title : 第十三界瑞麗模特大賽
         * type : 0
         * url : http://m.mv14449315.icoc.bz/index.jsp
         */

        private int aid;
        private String createtime;
        private String icon;
        private Object productId;
        private String title;
        private int type;
        private String url;

        public int getAid() {
            return aid;
        }

        public void setAid(int aid) {
            this.aid = aid;
        }

        public String getCreatetime() {
            return createtime;
        }

        public void setCreatetime(String createtime) {
            this.createtime = createtime;
        }

        public String getIcon() {
            return icon;
        }

        public void setIcon(String icon) {
            this.icon = icon;
        }

        public Object getProductId() {
            return productId;
        }

        public void setProductId(Object productId) {
            this.productId = productId;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public int getType() {
            return type;
        }

        public void setType(int type) {
            this.type = type;
        }

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }
    }
}

輪播圖的介面:https://www.zhaoapi.cn/ad/getAd

//介面和bean

//httputils

public class HttpUtil {

    private static volatile HttpUtil instance;
    public OkHttpClient okhttpClient;

    private HttpUtil() {
        if (okhttpClient == null) {
            synchronized (OkHttpClient.class) {
                if (null == okhttpClient) {
                    okhttpClient = new OkHttpClient();
                }
            }
        }
    }

    public static HttpUtil getInstance() {
        if (instance == null) {
            synchronized (HttpUtil.class) {
                if (null == instance) {
                    instance = new HttpUtil();
                }
            }
        }
        return instance;
    }

    public void get(String urlString, Callback callback) {
        Request request = new Request.Builder().url(urlString).build();
        okhttpClient.newCall(request).enqueue(callback);
    }


}

//httputils

//basemvp

public class BaseMVP  {


   public interface IPresenter<I> {
        void attch(IView iView);
        void detch();
        void request(String password, String text);
    }
    public interface IModel{
       void getData(String password, String text, OnCallBack onCallBack);
       interface OnCallBack{
           void result(String string);
       }
    }

    public interface IView{
       void showView(String json);
    }



    public interface IRegPresenter<I> {
        void attchReg(RegActivity regActivity);

        void detchReg();

        void regRequest();
    }

}

//basemvp

//Model
public class Model implements BaseMVP.IModel{

    private String url = "https://www.zhaoapi.cn/user/login";
    private static final String TAG = "Model";
    @Override
    public void getData(String password, String text, final OnCallBack onCallBack) {

        String URL = url+"?mobile="+text+"&password="+password;

        HttpUtil instance = HttpUtil.getInstance();
        instance.get(URL, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                String message = e.getMessage();
                onCallBack.result(message);
                Log.d(TAG, "onFailure: "+message);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String string = response.body().string();
                onCallBack.result(string);
                Log.d(TAG, "onResponse: "+string);
            }
        });
    }
}
//Model

 

//登陸的LoginPresenter
public class LoginPresenter implements BaseMVP.IPresenter<BaseMVP.IView> {

    BaseMVP.IView iView;
    private Model model;

    @Override
    public void attch(BaseMVP.IView iView) {
        this.iView = iView;
        model = new Model();

    }

    @Override
    public void detch() {
        if (iView != null){
            iView = null;
        }
    }

    @Override
    public void request(String password, String text) {
        model.getData(password,text, new BaseMVP.IModel.OnCallBack() {
            @Override
            public void result(String string) {
                iView.showView(string);
            }
        });
    }
}
//登陸的LoginPresenter
//RegPresenter
public class RegPresenter implements BaseMVP.IRegPresenter<BaseMVP.IView> {


    @Override
    public void attchReg(RegActivity regActivity) {

    }

    @Override
    public void detchReg() {

    }

    @Override
    public void regRequest() {

    }
}
//RegPresenter

//mainactivity

public class MainActivity extends AppCompatActivity {

    private FrameLayout frament;
    private RadioGroup radio;
    private RadioButton homepage;
    private RadioButton me;
    private FragmentManager manager;
    private HomePage homePage;
    private MeFragment meFragment;

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

        initView();

    }

    private void initView() {
        frament = findViewById(R.id.frament);
        radio = findViewById(R.id.radio);
        homepage = findViewById(R.id.homepage);
        me = findViewById(R.id.me);

        homePage = new HomePage();
        meFragment = new MeFragment();

        //預設選中頁面
        manager = getSupportFragmentManager();
        manager.beginTransaction().replace(R.id.frament, homePage).commit();

        //替換fragmnet
        radio.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                FragmentTransaction beginTransaction = manager.beginTransaction();
                switch (checkedId) {
                    case R.id.homepage:
                        beginTransaction.replace(R.id.frament, homePage);
                        break;
                    case R.id.me:
                        beginTransaction.replace(R.id.frament, meFragment);
                        break;

                }
                beginTransaction.commit();
            }
        });

    }
}

//mainactivity

//login的activity

  private EditText login_password;
    private EditText login_text;
    private BaseMVP.IPresenter<BaseMVP.IView> loginPresenter;

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

        loginPresenter = new LoginPresenter();
        loginPresenter.attch(this);


        initView();

    }

    private void initView() {
        Button login_loginbut = findViewById(R.id.login_loginbut);
        login_password = findViewById(R.id.login_password);
        login_text = findViewById(R.id.login_text);
        TextView login_reg = findViewById(R.id.login_reg);

        login_loginbut.setOnClickListener(this);
        login_reg.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.login_loginbut:

                String password = login_password.getText().toString();
                String text = login_text.getText().toString();
                loginPresenter.request(password,text);


                break;
            case R.id.login_reg:
                Intent intent = new Intent(LoginActivity.this,RegActivity.class);
                startActivity(intent);
                break;

        }
    }

    @Override
    public void showView(final String string) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Gson gson = new Gson();
                LoginBean loginBean = gson.fromJson(string, LoginBean.class);
                String msg = loginBean.getMsg();
                Toast.makeText(LoginActivity.this, msg+"", Toast.LENGTH_SHORT).show();
                if (msg.contains("登入成功")){
                    Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                    startActivity(intent);
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        loginPresenter.detch();
    }
}

//login的activity

//reg的activity

public class RegActivity extends AppCompatActivity implements BaseMVP.IView, View.OnClickListener {

    private EditText reg_password;
    private EditText reg_text;
    private BaseMVP.IPresenter<BaseMVP.IView> presenter;

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

        presenter = new LoginPresenter();
        presenter.attch(this);

        initView();
    }

    private void initView() {
        Button reg_but = findViewById(R.id.reg_but);
        reg_password = findViewById(R.id.reg_password);
        reg_text = findViewById(R.id.reg_text);

        reg_but.setOnClickListener(this);


    }


    @Override
    public void showView(String json) {

    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.reg_but:
                String password = reg_password.getText().toString();
                String text = reg_text.getText().toString();
                presenter.request("","");
                break;

        }
    }
}

//reg的activity

//首頁的

public class HomePage extends Fragment implements View.OnClickListener {


    private View inflate;
    private int REQUEST_CODE = 1000;
    private EditText code_edit;
    private ImageView code_img;
    private ViewPager banner;
    private String BannerURL = "https://www.zhaoapi.cn/ad/getAd";

    public HomePage() {
        // Required empty public constructor
    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0){
                int currentItem = banner.getCurrentItem();
                banner.setCurrentItem(currentItem+1);
                sendEmptyMessageDelayed(0,2000);
            }
        }
    };


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        inflate = inflater.inflate(R.layout.fragment_home_page, container, false);

        initView();

        return inflate;
    }

    private void initView() {
        banner = inflate.findViewById(R.id.banner);
        Button code_button = inflate.findViewById(R.id.code_button);
        code_edit = inflate.findViewById(R.id.code_edit);
        code_img = inflate.findViewById(R.id.code_img);
        TextView code = inflate.findViewById(R.id.code);


        code.setOnClickListener(this);
        code_button.setOnClickListener(this);

        //輪播圖
        HttpUtil instance = HttpUtil.getInstance();
        instance.get(BannerURL, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                String message = e.getMessage();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final String string = response.body().string();

                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Gson gson = new Gson();
                        BannerBean bannerBean = gson.fromJson(string, BannerBean.class);
                        List<BannerBean.DataBean> data = bannerBean.getData();
                        BannerAdapter bannerAdapter = new BannerAdapter(getActivity(),data);
                        banner.setAdapter(bannerAdapter);
                        handler.sendEmptyMessageDelayed(0,2000);
                    }
                });



            }
        });


    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.code:
                Intent intent = new Intent(getContext(), CaptureActivity.class);
                startActivityForResult(intent, REQUEST_CODE);
                break;
            case R.id.code_button:
                String edit_text = code_edit.getText().toString();
                if (TextUtils.isEmpty(edit_text)) {
                    Toast.makeText(getContext(), "您的輸入為空!", Toast.LENGTH_SHORT).show();
                    return;
                }
                code_edit.setText("");
               Bitmap mBitmap = CodeUtils.createImage(edit_text, 400, 400, null);
                code_img.setImageBitmap(mBitmap);
                break;



        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE) {
            //處理掃描結果(在介面上顯示)
            if (null != data) {
                Bundle bundle = data.getExtras();
                if (bundle == null) {
                    return;
                }
                if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) {
                    String result = bundle.getString(CodeUtils.RESULT_STRING);
                    Toast.makeText(getContext(), "解析結果:" + result, Toast.LENGTH_LONG).show();
                } else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) {
                    Toast.makeText(getContext(), "解析二維碼失敗", Toast.LENGTH_LONG).show();
                }
            }
        }
    }
}

//首頁的

//我的

public class MeFragment extends Fragment {


    private View inflate;
    private Button me_login;

    public MeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        inflate = inflater.inflate(R.layout.fragment_me, container, false);
        initView();
        return inflate;
    }

    private void initView() {
        me_login = inflate.findViewById(R.id.me_login);





        me_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), LoginActivity.class);
                startActivity(intent);
            }
        });


    }

}

//我的

//login.xml

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

    <EditText
        android:hint="請輸入手機號"
        android:id="@+id/login_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <EditText
        android:hint="請輸入密碼"
        android:id="@+id/login_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    
    <Button
        android:layout_gravity="center"
        android:id="@+id/login_loginbut"
        android:text="登入"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/login_reg"
        android:textColor="#0064EB"
        android:layout_marginTop="50dp"
        android:layout_marginLeft="400dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="註冊" />

</LinearLayout>

//login.xml

//main.xml

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

    <FrameLayout
        android:id="@+id/frament"
        android:layout_weight="9"
        android:layout_width="match_parent"
        android:layout_height="0dp">


    </FrameLayout>


    <RadioGroup
        android:id="@+id/radio"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="horizontal"
        >

        <RadioButton
            android:button="@null"
            android:text="首頁"
            android:id="@+id/homepage"
            android:gravity="center"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent" />

        <RadioButton
            android:button="@null"
            android:text="我的"
            android:id="@+id/me"
            android:gravity="center"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent" />

    </RadioGroup>

</LinearLayout>

//main.xml

//reg.xml

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

    <EditText
        android:id="@+id/reg_text"
        android:hint="請輸入手機號"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <EditText
        android:id="@+id/reg_password"
        android:hint="請輸入密碼"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:text="註冊"
        android:id="@+id/reg_but"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

//reg.xml

//首頁的xml

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

   <RelativeLayout
       android:background="#ED1C24"
       android:layout_width="match_parent"
       android:layout_height="40dp"
       >

       <TextView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_alignParentBottom="true"
           android:layout_centerHorizontal="true"
           android:gravity="center"
           android:layout_marginBottom="13dp"
           android:text="首頁" />

       <TextView
           android:id="@+id/code"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_alignParentBottom="true"
           android:layout_marginBottom="13dp"
           android:layout_marginRight="2dp"
           android:text="掃一掃"
           android:layout_alignParentRight="true" />

   </RelativeLayout>

   <android.support.v4.view.ViewPager
       android:id="@+id/banner"
       android:layout_width="match_parent"
       android:layout_height="200dp">

   </android.support.v4.view.ViewPager>


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


        <EditText
            android:background="@drawable/backgroup"
            android:id="@+id/code_edit"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:layout_alignTop="@+id/code_button"
            android:layout_alignParentLeft="true" />

        <Button
            android:id="@+id/code_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginTop="33dp"
            android:text="生成"
            android:layout_alignParentRight="true" />

        <ImageView
            android:id="@+id/code_img"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="98dp" />


    </RelativeLayout>


</LinearLayout>

//首頁的xml

//我的xml

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

    <RelativeLayout
        android:background="#ED1C24"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:gravity="center"
            android:layout_marginBottom="13dp"
            android:text="我的" />

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:background="#ED1C24">

        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_above="@+id/login"
            android:layout_centerHorizontal="true"
            android:src="@mipmap/ic_launcher" />

        <Button
            android:id="@+id/me_login"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="109dp"
            android:text="登入" />

    </RelativeLayout>

</LinearLayout>

我的xml