当前位置: 首页 > 工具软件 > ProgressHUD > 使用案例 >

Android框架之ProgressHUD,ButterKnife

张腾
2023-12-01

1.ProgressHUD多种加载效果

https://github.com/Kaopiz/KProgressHUD


2.ButterKnife 优势:

1.强大的View绑定和Click事件处理功能,简化代码,提升开发效率

2.方便的处理Adapter里的ViewHolder绑定问题

3.运行时不会影响APP效率,使用配置方便

4.代码清晰,可读性强

 https://github.com/JakeWharton/butterknife

ButterKnife自动生成插件安装:

AndroidStudio->File->Settings->Plugins->搜索Zelezny下载添加就行

右键——>Generate——Generate ButterKnife Injections

-------------------------------------------------------------------------------------------------------------------------------------

Activity中实现

package com.example.a10121581.myapplication.stateMachine;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.permissiondemo.R;
import com.kaopiz.kprogresshud.KProgressHUD;

import org.apache.http.params.HttpParams;

import butterknife.BindString;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import butterknife.Unbinder;

/**
 * Created by 10076418 on 2018/03/26.
 */

public class LoginActivity extends Activity {
    private final static String TAG = LoginActivity.class.getSimpleName();
    private Unbinder unbinder;
    @BindView(R.id.et_username)
    EditText et_username;
    @BindView(R.id.et_password)
    EditText et_password;
    @BindView(R.id.btn_login)
    Button btn_login;
    private String username;
    private String password;
    private KProgressHUD hud;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        unbinder = ButterKnife.bind(this);
        System.out.println(unbinder);
    }

    /**
     * 执行登录操作
     *
     * @param username2
     * @param password2
     */
    protected void sendLogin(String username2, String password2) {
        //设置为登录状态
        LoginContext.getInstance().setState(new LoginState());
        //--------KProgressHUD--begin-------------
        hud = KProgressHUD.create(LoginActivity.this)
                .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                .setLabel("Please wait")
                .setDetailsLabel("Downloading data")
                .setCancellable(true)
                .setAnimationSpeed(2)
                .setDimAmount(0.5f)
                .show();
        scheduleDismiss();
        //--------KProgressHUD--end-------------
        startActivity(new Intent(LoginActivity.this, MainActivity.class));
        Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
    }

    private void scheduleDismiss() {
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                hud.dismiss();
            }
        }, 5000);
    }

    @OnTextChanged(value = R.id.et_password, callback = OnTextChanged.Callback.BEFORE_TEXT_CHANGED)
    void beforeTextChanged(CharSequence s, int start, int count, int after) {
        Log.e(TAG, s.toString());
    }

    @OnTextChanged(value = R.id.et_password, callback = OnTextChanged.Callback.TEXT_CHANGED)
    void onTextChanged(CharSequence s, int start, int before, int count) {
        Log.e(TAG, s.toString());
    }

    @OnTextChanged(value = R.id.et_password, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
    void afterTextChanged(Editable s) {
        Log.e(TAG, s.toString());
    }


    @OnClick(R.id.btn_login)
    public void onButterKnifeBtnClick(View view) {
        Log.e(TAG, "onButterKnifeBtnClick");
        username = et_username.getEditableText().toString().trim();
        password = et_password.getEditableText().toString().trim();
        if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
            Toast.makeText(LoginActivity.this, "用户名密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        sendLogin(username, password);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
    }
}
app文件中
apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.example.a10121581.myapplication"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.0.0'
    compile 'com.android.support:support-v13:25.0.0'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.kaopiz:kprogresshud:1.1.0'
    compile 'com.jakewharton:butterknife:8.4.0'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
    testCompile 'junit:junit:4.12'
}

 类似资料: