Alert Dialog "Done"按鈕定義.

jackie_gnu發表於2011-08-11

 在預設情況下,當使用者按下AlertDialog 的"Done"按鈕時,Dialog會自動消失.但有些情況下我們需要它仍然存在不消失, 那如何處理呢?

請對照如下程式碼:

package com.example.test;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class SpinnerDemo extends Activity implements OnClickListener {
	private String TAG = "SpinnerDemo";
	private Button btn = null;
	private Button btn2 = null;
	private AlertDialog mAlertDialog = null;  ---> 儲存Dialog對物件

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		this.setContentView(R.layout.main);

		btn = (Button) findViewById(R.id.button1);
		//btn2 = (Button) findViewById(R.id.button2);

		btn.setOnClickListener(this);
		//btn2.setOnClickListener(this);

		super.onCreate(savedInstanceState);
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		showDialog(0);
		if (mAlertDialog != null) {
			Button bt = mAlertDialog.getButton(DialogInterface.BUTTON_POSITIVE); ---> 取得"Done"
			if (bt != null) {
/*				bt.setOnTouchListener(new OnTouchListener() {
					boolean outOfRange = false;

					public boolean onTouch(View v, MotionEvent event) {
						// TODO Auto-generated method stub
						Log.v(TAG, " onTouch..." );
						
						if (event.getAction() == MotionEvent.ACTION_DOWN) {
							Log.v(TAG, " ACTION_DOWN_..." );
							outOfRange = false;
							v.setPressed(true);
						} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
							Log.v(TAG, " ACTION_MOVE..." );
							boolean eventOnView = ((0 <= event.getX()) && (event
									.getX() <= v.getWidth()))
									&& ((0 <= event.getY()) && (event.getY() <= v
											.getHeight()));
							if (!eventOnView)
								outOfRange = true;
						} else if (event.getAction() == MotionEvent.ACTION_UP) {
							Log.v(TAG, " ACTION_UP..." );
							if (outOfRange == false)
								return false;

							v.cancelLongPress();
							v.setPressed(false);
							return true;
						}
						return false;
					}

				});
*/
				bt.setOnClickListener(new OnClickListener() { ---->重新定義"Done" OnClickListener

					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						Log.v(TAG, " onClick ..." );
//						dismissDialog(0);
						removeDialog(0);
					}
				});
			}
		}
	}

	@Override
	protected Dialog onCreateDialog(int id, Bundle args) {
		// TODO Auto-generated method stub
		return super.onCreateDialog(id, args);
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		// TODO Auto-generated method stub
		LayoutInflater factory = LayoutInflater.from(this);
		View editview = factory.inflate(R.layout.alter_dialog_text_entry, null);
		EditText et = (EditText) editview.findViewById(R.id.TextView01);

		Button btnDone = (Button) editview.findViewById(R.id.dialog_done);
		Button btnCancel = (Button) editview.findViewById(R.id.dialog_cancel);

		btnDone.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				dismissDialog(0);
			}

		});

		btnCancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

			}
		});

		AlertDialog.Builder builder = new AlertDialog.Builder(SpinnerDemo.this);
		builder.setTitle("Dialog Title");
		builder.setPositiveButton("Done",
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
					}
				});

		builder.setNegativeButton("Cancel",
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub

					}
				});

		builder.setView(editview);

		mAlertDialog = builder.create(); ----->儲存Dialog物件
		// return builder.create();
		return mAlertDialog;
		// return super.onCreateDialog(id);
	}

}


 

相關文章