MVP登录

 

Application

public class ErWeiMaApplication extends Application{
    @Override
    public void onCreate() {
        super.onCreate();
        ZXingLibrary.initDisplayOpinion(this);
    }
}

 

firstFragment

public class FirstFragment extends Fragment {

    public static final int REQUEST_CODE = 1;
    private TextView txtSao;
    private ViewPager vpLunBo;
    private EditText edText;
    private Button btnShengcheng;
    private ImageView imageLogo;
    private Bitmap mBitmap;
    private List<String> imgList;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 123){
                int current = vpLunBo.getCurrentItem();
                if (current<imgList.size() - 1){
                    current ++;
                }else {
                    current = 0;
                }
                vpLunBo.setCurrentItem(current);
                sendEmptyMessageDelayed(123,2000);
            }

        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view =  inflater.inflate(R.layout.fragment_first, container, false);
        txtSao = view.findViewById(R.id.txt_sao);
        vpLunBo = view.findViewById(R.id.vp_lunbo);
        btnShengcheng = view.findViewById(R.id.btn_shengcheng);
        imageLogo = view.findViewById(R.id.image_logo);
        edText = view.findViewById(R.id.ed_text);
        //轮播图
        imgList = new ArrayList<>();
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        /**
         * 打开默认二维码扫描界面
         */
        txtSao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), CaptureActivity.class);
                startActivityForResult(intent, REQUEST_CODE);
            }
        });
        /**
         * 生成二维码图片
         */
        btnShengcheng.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String textContent = edText.getText().toString();
                if (TextUtils.isEmpty(textContent)) {
                    Toast.makeText(getActivity(), "您的输入为空!", Toast.LENGTH_SHORT).show();
                    return;
                }
                edText.setText("");
                mBitmap = CodeUtils.createImage(textContent, 200, 200, BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
                imageLogo.setImageBitmap(mBitmap);
            }
        });
        //轮播
        imgList.add("http://01.imgmini.eastday.com/mobile/20180512/20180512072505_0fe08f494e7c090764244e3581b3e5ca_5_mwpm_03200403.jpg");
        imgList.add("http://01.imgmini.eastday.com/mobile/20180512/20180512072505_0fe08f494e7c090764244e3581b3e5ca_1_mwpm_03200403.jpg");
        imgList.add("http://06.imgmini.eastday.com/mobile/20180512/20180512_38f5183808987be3783b180740d12a2a_cover_mwpm_03200403.jpg");

        vpLunBo.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return imgList.size();
            }

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

            @NonNull
            @Override
            public Object instantiateItem(@NonNull ViewGroup container, int position) {
                ImageView img = new ImageView(getActivity());
                Glide.with(getActivity()).load(imgList.get(position)).into(img);
                container.addView(img);
                return img;
            }

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

        });
        handler.sendEmptyMessageDelayed(123,2000);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        handler.removeCallbacksAndMessages(null);
    }

    @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(getActivity(), "解析结果:" + result, Toast.LENGTH_LONG).show();
                } else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) {
                    Toast.makeText(getActivity(), "解析二维码失败", Toast.LENGTH_LONG).show();
                }
            }
        }
    }

}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.windows.zhoukao2_lianxi.fragment.FirstFragment">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/txt_first"
            android:layout_width="320dp"
            android:gravity="center"
            android:padding="10dp"
            android:background="@color/colorAccent"
            android:layout_height="wrap_content"
            android:text="首页"/>
        <TextView
            android:id="@+id/txt_sao"
            android:padding="10dp"
            android:gravity="right"
            android:background="@color/colorAccent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="扫一扫"/>
    </LinearLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/vp_lunbo"
        android:layout_width="match_parent"
        android:layout_height="200dp"></android.support.v4.view.ViewPager>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <EditText
            android:background="@drawable/radio"
            android:id="@+id/ed_text"
            android:layout_width="300dp"
            android:layout_height="wrap_content" />
        <Button
            android:id="@+id/btn_shengcheng"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="生成"/>
    </LinearLayout>
    <ImageView
        android:id="@+id/image_logo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

第二个fragment

public class MeFragment extends Fragment implements IView<LoginBean>, View.OnClickListener {

    private EditText etUsername;
    private EditText etPassword;
    private Button btnLogin;
    private LoginPresenter presenter;
    private ProgressDialog dialog;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view =  inflater.inflate(R.layout.fragment_me, container, false);
        etUsername = view.findViewById(R.id.et_username);
        etPassword = view.findViewById(R.id.et_password);
        btnLogin = view.findViewById(R.id.btn_login);
        btnLogin.setOnClickListener(this);
        
        initData();

        return view;
    }

    private void initData() {
        presenter = new LoginPresenter();
        presenter.attach(this);
    }

    @Override
    public void success(LoginBean loginBean) {
        if (loginBean != null){
            Toast.makeText(getActivity(), loginBean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void failed(Exception e) {
        Toast.makeText(getActivity(), "网络异常" + e.getMessage(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public String getUsername() {
        return etUsername.getText().toString().trim();
    }

    @Override
    public String getPassword() {
        return etPassword.getText().toString().trim();
    }

    @Override
    public void setUsername(String username) {
        etUsername.setText(username);
    }

    @Override
    public void setPassword(String password) {
        etPassword.setText(password);
    }
    
    @Override
    public void gotoMain() {
        Intent intent = new Intent(getActivity(), ShowActivity.class);
        startActivity(intent);
        getActivity().finish();
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        if (presenter != null){
            presenter.detach();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_login:
            presenter.login("https://www.zhaoapi.cn/user/login");
                break;
        }
    }
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.windows.zhoukao2_lianxi.fragment.MeFragment">

    <!-- TODO: Update blank fragment layout -->
    <EditText
        android:id="@+id/et_username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入用户名" />

    <EditText
        android:id="@+id/et_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入密码"
        android:inputType="textPassword" />

    <Button
        android:id="@+id/btn_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登录" />

</LinearLayout>

interface   ICalloBack

public interface ICalloBack {
    void onSuccess(Object obj);

    void onFailed(Exception e);
}
model

public class LoginModel {
    public void login(String url , ICalloBack calloBack , Type type){
        HttpUtils.getInstance().get(url,calloBack,type);
    }
}
presenter

public class LoginPresenter {

    private IView iv;
    private LoginModel loginModel;

    public void attach(IView iv) {
        this.iv = iv;
        loginModel = new LoginModel();
    }

    public void detach() {
        if (iv != null) {
            iv = null;
        }
    }
    public void login(String url) {
        final String username = iv.getUsername();
        final String password = iv.getPassword();

        url = url.concat("?mobile=").concat(username).concat("&password=").concat(password);
        Type type = new TypeToken<LoginBean>() {
        }.getType();
        loginModel.login(url, new ICalloBack() {
            @Override
            public void onSuccess(Object obj) {
                iv.success(obj);

                iv.gotoMain();
            }

            @Override
            public void onFailed(Exception e) {
                iv.failed(e);
            }
        }, type);
    }
}
OKHTTP

public class HttpUtils {

    private static volatile HttpUtils instance;

    private Handler handler = new Handler();
    private final OkHttpClient client;

    private HttpUtils(){
        client = new OkHttpClient();

    }

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

    public void get(String url, final ICalloBack callBack, final Type type) {
        Request request = new Request.Builder()
                .get()
                .url(url)
                .build();

        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, final IOException e) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        callBack.onFailed(e);
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                Gson gson = new Gson();
                final Object o = gson.fromJson(result, type);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        callBack.onSuccess(o);
                    }
                });
            }
        });
    }
}
view

public interface IView<T> {

    void success(T t);

    void failed(Exception e);

    String getUsername();

    String getPassword();

    void setUsername(String username);

    void setPassword(String password);

    /*void check(boolean isChecked);*/

    void gotoMain();

    Context getContext();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值