Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 繼承DialogFragment彈出dialog對話框二

Android 繼承DialogFragment彈出dialog對話框二

編輯:關於Android編程

之前寫過一篇關於Android 繼承DialogFragment彈出dialog對話框一,這次是在上次的基礎上修改了一些東西,就是怎樣在DialogFragment中獲取getDialog()是獲取當前對話框句柄。就可以進行布局可變的靈活操作。就像getactivity();一樣使用。下面看代碼。

 

MainActivity

 

package com.example.fragmentdialogdemo;

import com.example.fragmentdialogdemo.TestDialog.onTestListener;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends FragmentActivity implements OnClickListener,
		onTestListener {

	private String mstrName = ;
	private String mstrHigh = ;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		initUI();
	}

	private void initUI() {
		Button buttonTest = (Button) findViewById(R.id.buttonTest);
		buttonTest.setOnClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	// 接口回調的函數
	@Override
	public void onTestListener(int uniqueIdentifier, String strName,
			String strHigh) {
		if (uniqueIdentifier == 1) {
			Toast.makeText(getApplicationContext(),
					姓名: + strName + ,身高: + strHigh, Toast.LENGTH_LONG)
					.show();
			TextView textView1 = (TextView) findViewById(R.id.textView1);
			textView1.setText(姓名: + strName + ,身高: + strHigh);
		}

		mstrName = strName;
		mstrHigh = strHigh;
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.buttonTest:
			// 實例化TestDialog,可以傳參數進去,例如標題,或者其他參數,還有一個唯一碼.
			TestDialog dialog = new TestDialog().newInstance(請輸入, 1,
					mstrName, mstrHigh);
			dialog.show(this.getSupportFragmentManager(), TestDialog);
			break;
		default:
			break;
		}

	}

}

TestDialog

 

 

package com.example.fragmentdialogdemo;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class TestDialog extends DialogFragment implements OnCheckedChangeListener{

	// mUniqueFlag作用是唯一碼,可以使返回時做判斷
	private int mUniqueFlag = -1;
	private onTestListener mOnListener;
	private EditText meditTextName, meditTextHigh;
	protected Button mButtonPositive;

	/**
	 * 新建實例
	 * 
	 * @param title
	 * @param unique
	 * @param strName
	 * @param strHigh
	 * @return
	 */
	public static TestDialog newInstance(String title, int unique,
			String strName, String strHigh) {
		TestDialog tDialog = new TestDialog();
		Bundle args = new Bundle();
		args.putString(SelectTemplateTitle, title);
		args.putInt(MultipleTemplate, unique);
		args.putString(TemplateName, strName);
		args.putString(TemplateHigh, strHigh);
		tDialog.setArguments(args);
		return tDialog;

	}

	public interface onTestListener {

		/**
		 * 
		 * @param uniqueIdentifier
		 *            唯一標識
		 * @param strName
		 * @param strHigh
		 */
		public abstract void onTestListener(int uniqueIdentifier,
				String strName, String strHigh);
	}

	// 旋轉時候保存
	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putString(InputName, meditTextName.getText().toString());
		outState.putString(InputHigh, meditTextHigh.getText().toString());
	}

	@Override
	public Dialog onCreateDialog(Bundle saveInstanceState) {
		String title = getArguments().getString(SelectTemplateTitle);
		mUniqueFlag = getArguments().getInt(MultipleTemplate);

		AlertDialog.Builder Builder = new AlertDialog.Builder(getActivity())
				.setTitle(title)
				.setPositiveButton(確定, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						
						// 觸發數據回調
						if (mOnListener != null)
							mOnListener.onTestListener(mUniqueFlag,
									meditTextName.getText().toString(),
									meditTextHigh.getText().toString());
					}
				}).setNegativeButton(取消, null);

		// 添加xml布局
		View view = getActivity().getLayoutInflater().inflate(
				R.layout.test_dialog, null);
		setupUI(view);

		// 旋轉後,恢復數據
		if (saveInstanceState != null) {
			String strName = saveInstanceState.getString(InputName);
			if (strName != null)
				meditTextName.setText(strName);

			String strHigh = saveInstanceState.getString(InputHigh);
			if (strHigh != null)
				meditTextHigh.setText(strHigh);
		}
		Builder.setView(view);

		//創建對話框
		AlertDialog dialog = (AlertDialog) Builder.create();
		return dialog;
	}

	private void setupUI(View view) {
		if (view == null)
			return;
		
		RadioGroup radioGroup1 = (RadioGroup)view.findViewById(R.id.radioGroup1);
		radioGroup1.setOnCheckedChangeListener(this);
		
		String strName = getArguments().getString(TemplateName);
		String strHigh = getArguments().getString(TemplateHigh);
		meditTextName = (EditText) view.findViewById(R.id.editTextName);
		meditTextHigh = (EditText) view.findViewById(R.id.editTextHigh);

		meditTextName.setText(strName);
		meditTextHigh.setText(strHigh);
	}

	// onAttach是關聯activity的,用接口回調
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			mOnListener = (onTestListener) activity;
		} catch (ClassCastException e) {
			dismiss();
		}
	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		if (group.getId() == R.id.radioGroup1) {
			switch (checkedId) {
			case R.id.radio0:
				//getDialog()是獲取當前對話框
				getDialog().findViewById(R.id.LayoutName).setVisibility(View.VISIBLE);
				getDialog().findViewById(R.id.LayoutHigh).setVisibility(View.VISIBLE);
				break;
			case R.id.radio1:
				getDialog().findViewById(R.id.LayoutName).setVisibility(View.GONE);
				getDialog().findViewById(R.id.LayoutHigh).setVisibility(View.VISIBLE);			
				break;
			default:
				break;
			}			
		}
		
	}

}
activity_main

 

 



    

 

test_dialog

 



    

        

        

    

    

        

        

            
        
    

    

        

        

            
        
    

二、效果看下面動態圖

 

\

三、DialogFragment的部分方法

\

還是那句話多實踐,多總結,多交流!期待你的留言。
 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved