android介面回撥詳解、簡單例子+複雜例子
阿新 • • 發佈:2019-02-18
package test.ban.com.callback;/** * Created by apple on 16/8/30. */ import android.annotation.SuppressLint; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; /** * 作者:ban on 16/8/30 16:10 */ public class MainActivity extends Activity implements ImageStateInterface { private Button button; private TextView mTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /** * 例項化控制元件 */ //實現方式一 (推薦) onLincoln(); //實現方式二 //onLincolnTwo(); } /** * 實現方式一 這個需要 implements ImageStateInterface 介面 */ private void onLincoln() { button = (Button) findViewById(R.id.button); mTextView = (TextView) findViewById(R.id.tv); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { DownloadImageUtil downloadImageUtil = new DownloadImageUtil(); /** 呼叫StartDownLoad方法,目的是將MainActivity註冊到介面那裡, 使介面知道,需要呼叫的是哪個類中的實現該介面的方法。 */ downloadImageUtil.StartDownLoad(MainActivity.this, getApplicationContext()); } }); } @Override public void onImageStart() { Log.d("lincoln", "onImageStart"); button.setText("onImageStart"); mTextView.setText("onImageStart"); } @Override public void onImageSuccess(final Bitmap bitmap) { Log.d("lincoln", "onImageSuccess"); runOnUiThread(new Runnable() { @SuppressLint("NewApi") @Override public void run() { button.setText("onImageSuccess"); mTextView.setText("onImageSuccess"); button.setBackground(new BitmapDrawable(bitmap)); } }); } @Override public void onImageFailed() { } @Override public void OnEnd() { Toast.makeText(MainActivity.this, "完成啦!!", Toast.LENGTH_SHORT).show(); } /**---------------------實現方式一 結束------------------------*/ /** * --------------------------實現方式二----------------------------- */ private void onLincolnTwo() { button = (Button) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { DownloadImageUtil.StartDownLoad(imageInterface, MainActivity.this); } }); } ImageStateInterface imageInterface = new ImageStateInterface() { @Override public void onImageStart() { Log.d("lincoln", "onImageStart"); button.setText("onImageStart"); } @Override public void onImageSuccess(final Bitmap bitmap) { Log.d("lincoln", "onImageSuccess"); runOnUiThread(new Runnable() { @SuppressLint("NewApi") @Override public void run() { button.setText("onImageSuccess"); button.setBackground(new BitmapDrawable(bitmap)); } }); } @Override public void onImageFailed() { } @Override public void OnEnd() { Toast.makeText(MainActivity.this, "哈哈!!", Toast.LENGTH_SHORT).show(); } }; /**-----------------------------------------------------------------------------*/ }
原始碼地址:http://download.csdn.net/detail/u010566681/9617131