Android 自訂對話方塊,進度條,下拉重新整理等

來源:互聯網
上載者:User

標籤:對話方塊   開源項目   源碼   結構   

這個demo集合了自訂對話方塊,進度條,下拉重新整理以及popup彈出框等。是學習了網上開源項目後,抽取集合了常用對話方塊,進度條,下拉重新整理以及popup彈出框等。現在結構目錄簡單,更易於學習和擴充使用。注釋都卸載代碼。下面進行簡單的介紹以及部分代碼展示。

本文demo下載:點擊

1、整體實現的


2、項目結構圖


這上面項目結構圖也是一目瞭然,有什麼功能展示。大家也看到了,這上面類有點多,如果全部貼出來,不大可能,有興趣下載本文源碼。

3、看看基礎類BaseActivity

我就貼一下基礎類,還有實作類別。因為這些一段段代碼易於其他項目使用。

package com.org.base;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import com.example.activity.R;import com.example.activity.R.layout;import com.org.dialog.LoadingDialog;import com.org.view.HandyTextView;import android.os.AsyncTask;import android.os.Bundle;import android.util.DisplayMetrics;import android.util.Log;import android.view.Gravity;import android.view.LayoutInflater;import android.view.View;import android.widget.Toast;import android.app.Activity;import android.content.Intent;public class BaseActivity extends Activity {protected LoadingDialog mLoadingDialog;/** * 螢幕的寬度、高度、密度 */protected int mScreenWidth;protected int mScreenHeight;protected float mDensity;//非同步載入任務的listprotected List<AsyncTask<Void, Void, Boolean>> mAsyncTasks = new ArrayList<AsyncTask<Void, Void, Boolean>>();@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mLoadingDialog = new LoadingDialog(this, "請求提交中");DisplayMetrics metric = new DisplayMetrics();getWindowManager().getDefaultDisplay().getMetrics(metric);mScreenWidth = metric.widthPixels;mScreenHeight = metric.heightPixels;mDensity = metric.density;}protected void putAsyncTask(AsyncTask<Void, Void, Boolean> asyncTask) {mAsyncTasks.add(asyncTask.execute());}protected void clearAsyncTask() {Iterator<AsyncTask<Void, Void, Boolean>> iterator = mAsyncTasks.iterator();while (iterator.hasNext()) {AsyncTask<Void, Void, Boolean> asyncTask = iterator.next();if (asyncTask != null && !asyncTask.isCancelled()) {asyncTask.cancel(true);}}mAsyncTasks.clear();}protected void showLoadingDialog(String text) {if (text != null) {mLoadingDialog.setText(text);}mLoadingDialog.show();}protected void dismissLoadingDialog() {if (mLoadingDialog.isShowing()) {mLoadingDialog.dismiss();}}/** 短暫顯示Toast提示(來自res) **/protected void showShortToast(int resId) {Toast.makeText(this, getString(resId), Toast.LENGTH_SHORT).show();}/** 短暫顯示Toast提示(來自String) **/protected void showShortToast(String text) {Toast.makeText(this, text, Toast.LENGTH_SHORT).show();}/** 長時間顯示Toast提示(來自res) **/protected void showLongToast(int resId) {Toast.makeText(this, getString(resId), Toast.LENGTH_LONG).show();}/** 長時間顯示Toast提示(來自String) **/protected void showLongToast(String text) {Toast.makeText(this, text, Toast.LENGTH_LONG).show();}/** 顯示自訂Toast提示(來自res) **/protected void showCustomToast(int resId) {View toastRoot = LayoutInflater.from(BaseActivity.this).inflate(R.layout.common_toast, null);((HandyTextView) toastRoot.findViewById(R.id.toast_text)).setText(getString(resId));Toast toast = new Toast(BaseActivity.this);toast.setGravity(Gravity.CENTER, 0, 0);toast.setDuration(Toast.LENGTH_SHORT);toast.setView(toastRoot);toast.show();}/** 顯示自訂Toast提示(來自String) **/protected void showCustomToast(String text) {View toastRoot = LayoutInflater.from(BaseActivity.this).inflate(R.layout.common_toast, null);((HandyTextView) toastRoot.findViewById(R.id.toast_text)).setText(text);Toast toast = new Toast(BaseActivity.this);toast.setGravity(Gravity.CENTER, 0, 0);toast.setDuration(Toast.LENGTH_SHORT);toast.setView(toastRoot);toast.show();}/** Debug輸出Log日誌 **/protected void showLogDebug(String tag, String msg) {Log.d(tag, msg);}/** Error輸出Log日誌 **/protected void showLogError(String tag, String msg) {Log.e(tag, msg);}/** 通過Class跳轉介面 **/protected void startActivity(Class<?> cls) {startActivity(cls, null);}/** 含有Bundle通過Class跳轉介面 **/protected void startActivity(Class<?> cls, Bundle bundle) {Intent intent = new Intent();intent.setClass(this, cls);if (bundle != null) {intent.putExtras(bundle);}startActivity(intent);}/** 預設退出 **/protected void defaultFinish() {super.finish();}}

4、BasePopupWindow

package com.org.base;import android.annotation.TargetApi;import android.content.Context;import android.graphics.drawable.BitmapDrawable;import android.os.Build;import android.util.AttributeSet;import android.view.Gravity;import android.view.View;import android.widget.PopupWindow;@TargetApi(Build.VERSION_CODES.HONEYCOMB)public abstract class BasePopupWindow extends PopupWindow {protected View mContentView;protected onSubmitClickListener mOnSubmitClickListener;public BasePopupWindow() {super();}public BasePopupWindow(Context context, AttributeSet attrs,int defStyleAttr, int defStyleRes) {super(context, attrs, defStyleAttr, defStyleRes);}public BasePopupWindow(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);}public BasePopupWindow(Context context, AttributeSet attrs) {super(context, attrs);}public BasePopupWindow(Context context) {super(context);}public BasePopupWindow(int width, int height) {super(width, height);}public BasePopupWindow(View contentView, int width, int height,boolean focusable) {super(contentView, width, height, focusable);}public BasePopupWindow(View contentView) {super(contentView);}@SuppressWarnings("deprecation")public BasePopupWindow(View contentView, int width, int height) {super(contentView, width, height, true);mContentView = contentView;setBackgroundDrawable(new BitmapDrawable());initViews();initEvents();init();}public abstract void initViews();public abstract void initEvents();public abstract void init();public View findViewById(int id) {return mContentView.findViewById(id);}/** * 顯示在parent的上部並水平置中 *  * @param parent */public void showViewTopCenter(View parent) {showAtLocation(parent, Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 0);}/** * 顯示在parent的中心 *  * @param parent */public void showViewCenter(View parent) {showAtLocation(parent, Gravity.CENTER, 0, 0);}/** * 添加確認單擊監聽 *  * @param l */public void setOnSubmitClickListener(onSubmitClickListener l) {mOnSubmitClickListener = l;}public interface onSubmitClickListener {void onClick();}}

5、BaseArrayListAdapter
package com.org.base;import java.util.ArrayList;import java.util.Arrays;import java.util.List;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;//基本的適配器public class BaseArrayListAdapter extends BaseAdapter {protected Context mContext;protected LayoutInflater mInflater;protected List<String> mDatas = new ArrayList<String>();//String數組添加資料來源public BaseArrayListAdapter(Context context, String... datas) {mContext = context;mInflater = LayoutInflater.from(context);if (datas != null && datas.length > 0) {mDatas = Arrays.asList(datas);}}//用list方式添加資料來源public BaseArrayListAdapter(Context context, List<String> datas) {mContext = context;mInflater = LayoutInflater.from(context);if (datas != null && datas.size() > 0) {mDatas = datas;}}@Overridepublic int getCount() {return mDatas.size();}@Overridepublic Object getItem(int position) {return mDatas.get(position);}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {return null;}}
6、實現自訂對話方塊,進度條以及popup彈出框,主MainActivity

package com.example.activity;import com.org.base.BaseActivity;import com.org.dialog.EditTextDialog;import com.org.dialog.SimpleListDialog;import com.org.dialog.SimpleListDialog.onSimpleListItemClickListener;import com.org.popupwindow.ChatPopupWindow;import com.org.popupwindow.ChatPopupWindow.onChatPopupItemClickListener;import com.viw.adapt.CheckListDialogAdapter;import android.R.layout;import android.content.DialogInterface;import android.os.Bundle;import android.os.Handler;import android.util.TypedValue;import android.view.Gravity;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup.LayoutParams;import android.widget.Button;import android.widget.RelativeLayout;public class MainActivity extends BaseActivity implements OnClickListener,onChatPopupItemClickListener{protected SimpleListDialog mDialog;private int mCheckId = 0;private ChatPopupWindow mChatPopupWindow;private int mWidth;private int mHeaderHeight;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);findViewById(R.id.button1).setOnClickListener(this);findViewById(R.id.button2).setOnClickListener(this);findViewById(R.id.button3).setOnClickListener(this);findViewById(R.id.button4).setOnClickListener(this);findViewById(R.id.button5).setOnClickListener(this);initPopupWindow();}public void onSimpleListItem() {String[] modes = new String[]{"測試一","測試二","測試三"};mDialog = new SimpleListDialog(this);mDialog.setTitle("單選框");mDialog.setTitleLineVisibility(View.GONE);mDialog.setAdapter(new CheckListDialogAdapter(mCheckId, this, modes));//用匿名內部類實現介面監聽mDialog.setOnSimpleListItemClickListener(new DialogItemClickListener());mDialog.show();}protected class DialogItemClickListener implementsonSimpleListItemClickListener {@Overridepublic void onItemClick(int position) {mCheckId = position;}}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.button1:showLoadingDialog("正在載入。。。");new Handler().postDelayed(new Runnable() {@Overridepublic void run() {dismissLoadingDialog();}}, 10000);break;case R.id.button2:onSimpleListItem();break;case R.id.button3:onEditTextDialog();break;case R.id.button4:RelativeLayout layoutShow = (RelativeLayout)findViewById(R.id.layoutShow);mChatPopupWindow.showAtLocation(layoutShow, Gravity.RIGHT| Gravity.TOP, -10, mHeaderHeight + 10);break;case R.id.button5:startActivity(PullRefreshListViewActivity.class);break;default:break;}}private void onEditTextDialog() {final EditTextDialog mEditTextDialog = new EditTextDialog(this);mEditTextDialog.setTitle("填寫推薦人");mEditTextDialog.setButton("取消",new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {mEditTextDialog.cancel();}}, "確認", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {String text = mEditTextDialog.getText();if (text == null) {mEditTextDialog.requestFocus();} else {mEditTextDialog.dismiss();}}});mEditTextDialog.show();}//初始化PopupWindowprotected void initPopupWindow() {mWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,130, getResources().getDisplayMetrics());mHeaderHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 48, getResources().getDisplayMetrics());mChatPopupWindow = new ChatPopupWindow(this, mWidth,LayoutParams.WRAP_CONTENT);mChatPopupWindow.setOnChatPopupItemClickListener(this);}//以下是popupwindow的選擇監聽事件@Overridepublic void onVoiceModeClick() {showCustomToast("pop監聽1");}@Overridepublic void onCreateClick() {showCustomToast("pop監聽2");}@Overridepublic void onSynchronousClick() {showCustomToast("pop監聽3");}}

7、下拉重新整理PullRefreshListViewActivity
package com.example.activity;import android.os.AsyncTask;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import com.org.base.BaseActivity;import com.org.view.PullRefreshListView;import com.org.view.PullRefreshListView.OnCancelListener;import com.org.view.PullRefreshListView.OnRefreshListener;import com.viw.adapt.CheckListDialogAdapter;public class PullRefreshListViewActivity extends BaseActivityimplementsOnRefreshListener,OnCancelListener,OnItemClickListener{private PullRefreshListView mRefreshListView;String[] modes = new String[]{"測試一","測試二","測試三"};@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.activity_refresh);initViews();}protected void initViews() {mRefreshListView = (PullRefreshListView) findViewById(R.id.otherfeedlist_mmrlv_list);//適配器我隨便用了一個mRefreshListView.setAdapter(new CheckListDialogAdapter(1, this, modes));mRefreshListView.setOnItemClickListener(this);mRefreshListView.setOnRefreshListener(this);mRefreshListView.setOnCancelListener(this);}@Overridepublic void onCancel() {//取消重新整理mRefreshListView.onRefreshComplete();}@Overridepublic void onRefresh() {//非同步載入線程putAsyncTask(new AsyncTask<Void, Void, Boolean>() {@Overrideprotected Boolean doInBackground(Void... params) {try {Thread.sleep(2000);} catch (InterruptedException e) {}return null;}@Overrideprotected void onPostExecute(Boolean result) {super.onPostExecute(result);//這裡寫重新整理adapt資料就行可以了mRefreshListView.onRefreshComplete();}});}@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position,long id) {// listview的單擊事件就不寫了}}

本文demo下載:點擊





著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

Android 自訂對話方塊,進度條,下拉重新整理等

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.