Dialog、Toast、PopupWindow及Log分析

本文分析Dialog的原理,其用到的設計模式,其各種子類,及在Activity中的使用。Toast、Log跟Dialog一樣都有提示的功能,所以放一起分析。

1、Dialog實現了4個接口

1) android.content.DialogInterface

包含幾個常量,cancel和dismiss方法,

6個嵌套接口OnCancelListener,OnDismissListener,OnShowListener,OnClickListener,
OnMultiChoiceClickListener,OnKeyListener。
2) android.view.Window.Callback
3) android.view.KeyEvent.Callback
4) android.view.View.OnCreateContextMenuListener

2、AlertDialog實現

new AlertDialog.Builder(this)
	.setTitle("very good")
	.create()//實例化AlertDialog
	.show();
上面AlertDialog的實現用了建造者模式。

AlertDialog、AlertDialog.Builder分別適配了com.android.internal.app.AlertController和AlertController.AlertParams,用了對象的適配器模式。

final String[] items = {"I", "II", "III", "aVR", "aVL", "aVF", "V1", "V2", "V3", "V4", "V5", "V6"};
final SharedPreferences sp = getContext().getApplicationContext().getSharedPreferences("ecg12", Context.MODE_PRIVATE);
DialogInterface.OnClickListener onClickListener = new DialogInterface.OnClickListener() {
	//每次點擊列表項將設置值保留
	int mWhich;
	@Override
	public void onClick(DialogInterface dialog, int which) {
		//-1,-2,-3不會和index發生衝突
		if(which==DialogInterface.BUTTON_POSITIVE){
			sp.edit().putInt("ecg12_index", mWhich).apply();
			dialog.dismiss();
		}else{
			mWhich = which;
		}
	}
};
new AlertDialog.Builder(getContext())
	.setSingleChoiceItems(items, sp.getInt("ecg12_index",0), onClickListener)
	.setTitle("尋峯導聯設置")
	.setIcon(R.drawable.settings_ecg)
	.setPositiveButton(R.string.confirm, onClickListener).show();

3、Toast

一般通過Toast.makeText(Context context, CharSequence text, int duration).show()來使用Toast。

也可直接實例化使用:

Toast toast = new Toast(MainActivity.this);
TextView tv = new TextView(MainActivity.this);
tv.setText("嘻嘻");
toast.setView(tv);
toast.show();

Toast包含內部類private static class TN extends ITransientNotification.Stub作爲其屬性,並封裝了它的方法。

TN包含Handler屬性,Handler實例化需要之前調用Looper.prepare(),否則new Handler()時報異常。


4、Log

Log只含有私有無參構造private Log(),所以禁止直接實例化。

Log.v(String tag, String msg)

Log.e(String tag, String msg, Throwable tr):可打印異常信息
Log.println(int priority, String tag, String msg)

priority區分優先級,用不同顏色顯示,如Log.VERBOSE,Log.WARN,Log.ASSERT等。

System.out.println("Good");tag爲System.out,level爲i,priority爲INFO
System.err.println("Bad");tag爲System.err,level爲w,priority爲WARN

5、PopupWindow

TextView textView = new TextView(MainActivity.this);
textView.setText("so long");
mPopupWindow = new PopupWindow(textView, ViewGroup.LayoutParams.WRAP_CONTENT, 
		ViewGroup.LayoutParams.WRAP_CONTENT);
mPopupWindow.showAtLocation((View) v.getParent(), Gravity.BOTTOM, 0, 0);
mPopupWindow.dismiss();
6、比較

Log在LogCat中顯示,方便調試時查看,實現過濾、保存、刪除。


Toast在指定時間到達後自動消失。

Dialog會擋住當前Activity,PopupWindow不會。

7、示例演示Dialog的各種用法

1)MainActivity.java

public class MainActivity extends FragmentActivity {
	private static final String TAG = MainActivity.class.getSimpleName();
	public static final int DIALOG_ID_TEST = 0x01;
	EditText editText;
	Editable editable;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		editText = new EditText(this);
		editText.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
		editable = Editable.Factory.getInstance().newEditable("WhyWhatHow");
	}
	public void onClick(View v){
		Calendar calendar = Calendar.getInstance();
		switch (v.getId()) {
		case R.id.btn_dialog:
			Bundle bundle = new Bundle();
			bundle.putString("Content", new SimpleDateFormat("yyyy/MM/dd HH-mm-ss E").format(new Date()));
			showDialog(DIALOG_ID_TEST, bundle);break;
		case R.id.btn_char:
			CharacterPickerDialog characterPickerDialog = new CharacterPickerDialog(this, editText, editable, "123456789", false){
				public void onClick(View v) {
					dismiss();
					Log.i(TAG, "onClick-"+editText.getText());
				};
				@Override
				public void onItemClick(AdapterView parent, View view, int position, long id) {
					Log.i(TAG, "onItemClick-"+position);
				};
			};
			characterPickerDialog.show();break;
		case R.id.btn_date:
			int year = calendar.get(Calendar.YEAR);
			int month = calendar.get(Calendar.MONTH);
			int day = calendar.get(Calendar.DAY_OF_MONTH);
			DatePickerDialog dialog = new DatePickerDialog(this, mOnDateSetListener, year, month, day);
			if(android.os.Build.VERSION.SDK_INT>=11){
				dialog.getDatePicker().setCalendarViewShown(false);
				dialog.getDatePicker().setMaxDate(new Date().getTime());
				//1900年1月1號
				dialog.getDatePicker().setMinDate(new Date(0, 0, 1).getTime());
			}
			dialog.show();break;
		case R.id.btn_time:
			int h = calendar.get(Calendar.HOUR_OF_DAY);
			int m = calendar.get(Calendar.MINUTE);
			TimePickerDialog timePickerDialog = new TimePickerDialog(this, mTimeSetListener, h, m, true);
			timePickerDialog.show();break;
		case R.id.btn_progress:
			ProgressDialog progressDialog = new ProgressDialog(this);
			progressDialog.setMax(100);
			progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
			progressDialog.show();break;
		case R.id.btn_DialogFragment:
			DialogFragment dialogFragment = MyDialogFragment.getInstance(
				new SimpleDateFormat("yyyy/MM/dd HH-mm-ss E").format(new Date()));
			dialogFragment.show(getSupportFragmentManager(), "fantastic");break;
		case R.id.btn_Activity://Activity作爲Dialog使用
			startActivity(new Intent(this, DialogDemo.class));
		default:
			break;
		}
	}
	@Override
	protected Dialog onCreateDialog(int id, Bundle args) {
		//首次showDialog纔會觸發
		Log.i(TAG, "onCreateDialog");
		switch (id) {
		case DIALOG_ID_TEST:
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle("The current time is ...")
				.setMessage("The present");
			return builder.create();

		default:
			break;
		}
		return null;
	}
	@Override
	protected void onPrepareDialog(int id, Dialog dialog, Bundle args) {
		//每次showDialog都會觸發
		Log.i(TAG, "onPrepareDialog");
		switch (id) {
		case DIALOG_ID_TEST:
			((AlertDialog)dialog).setMessage(args.getString("Content"));
			break;

		default:
			break;
		}
	}
	private DatePickerDialog.OnDateSetListener mOnDateSetListener = new DatePickerDialog.OnDateSetListener() {
		@Override
		public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth) {
			Log.i(TAG, "onDateSet-"+year+"-"+monthOfYear+"-"+dayOfMonth);
		}
	};
	private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
		@Override
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			Log.i(TAG, "onTimeSet-"+hourOfDay+"-"+minute);
		}
	};
}
2)activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <Button 
        android:id="@+id/btn_dialog"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="dialog"/>
    <Button 
        android:id="@+id/btn_Activity"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="Activity"/>
    <Button 
        android:id="@+id/btn_date"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="date"/>
    <Button 
        android:id="@+id/btn_time"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="time"/>
    <Button 
        android:id="@+id/btn_char"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="char"/>
    <Button 
        android:id="@+id/btn_progress"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="progress"/>
    <Button 
        android:id="@+id/btn_DialogFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="DialogFragment"/>
</LinearLayout>
3)MyDialogFragment.java
/**
 * API level 4(API level 11引入)
 * 高效封裝和管理Dialog的lifecycle,和Dialog狀態一致
 * override either onCreateDialog or onCreateView
 */
public class MyDialogFragment extends DialogFragment {
	private static final String TAG = MyDialogFragment.class.getSimpleName();
	private static final String CURRENT_TIME = "CURRENT_TIME";
	public static MyDialogFragment getInstance(String currentTime){
		MyDialogFragment fragment = new MyDialogFragment();
		Bundle bundle = new Bundle();
		bundle.putString(CURRENT_TIME, currentTime);
		fragment.setArguments(bundle);
		return fragment;
	}
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		Log.i(TAG, "onCreateDialog");
//		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
//		builder.setTitle("The current time is ...")
//			.setMessage(getArguments().getString(CURRENT_TIME));
//		return builder.create();
		Dialog dialog = new MyDialog(getActivity());
		dialog.setTitle("Come on baby!");
		dialog.getWindow().setContentView(R.layout.dialog);
		EditText editText = (EditText) dialog.getWindow().findViewById(R.id.editText);
		editText.setHint("input something");
		return dialog;
	}
//	@Override
//	public View onCreateView(LayoutInflater inflater, ViewGroup container,
//			Bundle savedInstanceState) {
//		View view = inflater.inflate(R.layout.dialog_view, container, false);
//		TextView tv = (TextView) view.findViewById(R.id.dialog_view_text);
//		tv.setText("The girl is my lover!");
//		return view;
//	}
	@Override
	public void onCreate(Bundle savedInstanceState) {
		Log.i(TAG, "onCreate");
		super.onCreate(savedInstanceState);
	}
	@Override
	public void onStart() {
		Log.i(TAG, "onStart");
		super.onStart();
	}
	@Override
	public void onStop() {
		Log.i(TAG, "onStop");
		super.onStop();
	}
	@Override
	public void onDestroy() {
		Log.i(TAG, "onDestroy");
		super.onDestroy();
	}
}
4)MyDialog.java
/**
 * lifecycle of Dialog
 */
public class MyDialog extends Dialog {
	private static final String TAG = MyDialog.class.getSimpleName();
	public MyDialog(Context context) {
		super(context);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.i(TAG, "onCreate");
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void onStart() {
		Log.i(TAG, "onStart");
		super.onStart();
	}

	@Override
	protected void onStop() {
		Log.i(TAG, "onStop");
		super.onStop();
	}
}
5)dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <EditText 
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>
6)dialog_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <TextView 
        android:id="@+id/dialog_view_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>
7)DialogDemo.java
/**
 * 作爲Dialog使用
 */
public class DialogDemo extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_dialog);
	}
}
8)activity_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <TextView 
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Activity"/>
</LinearLayout>
9)AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.dialogtest3"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.dialogtest3.MainActivity"
            android:label="@string/app_name"
            android:launchMode="singleTask" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.example.dialogtest3.DialogDemo"
            android:theme="@android:style/Theme.Dialog"></activity>
    </application>

</manifest>


發佈了56 篇原創文章 · 獲贊 0 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章