標籤:android 布局 對話方塊
之前寫過一篇關於Android 繼承DialogFragment彈出dialog對話方塊一,這次是在上次的基礎上修改了一些東西,就是怎樣在DialogFragment中擷取getDialog()是擷取目前的交談框控制代碼。就可以進行布局可變的靈活操作。就像getactivity();一樣使用。下面看代碼。
本文demo:點擊
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 = "";@Overrideprotected 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);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.main, menu);return true;}// 介面回調的函數@Overridepublic 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;}@Overridepublic 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);}// 旋轉時候儲存@Overridepublic void onSaveInstanceState(Bundle outState) {super.onSaveInstanceState(outState);outState.putString("InputName", meditTextName.getText().toString());outState.putString("InputHigh", meditTextHigh.getText().toString());}@Overridepublic 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() {@Overridepublic 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的,用介面回調@Overridepublic void onAttach(Activity activity) {super.onAttach(activity);try {mOnListener = (onTestListener) activity;} catch (ClassCastException e) {dismiss();}}@Overridepublic 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
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="80dp" android:gravity="center" android:textSize="18sp" android:text="點擊button" /> <Button android:id="@+id/buttonTest" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /></LinearLayout>
test_dialog
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <RadioGroup android:id="@+id/radioGroup1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioButton android:id="@+id/radio0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="姓名" /> <RadioButton android:id="@+id/radio1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="身高" /> </RadioGroup> <LinearLayout android:id="@+id/LayoutName" android:layout_width="match_parent" android:layout_height="60dp" > <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="left|center_vertical" android:text="姓名:" android:textSize="18sp" /> <EditText android:id="@+id/editTextName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" > <requestFocus /> </EditText> </LinearLayout> <LinearLayout android:id="@+id/LayoutHigh" android:layout_width="match_parent" android:layout_height="60dp" > <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="left|center_vertical" android:text="身高:" android:textSize="18sp" /> <EditText android:id="@+id/editTextHigh" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" > <requestFocus /> </EditText> </LinearLayout></LinearLayout>
二、效果看下面動態圖
三、DialogFragment的部分方法
還是那句話多實踐,多總結,多交流!期待你的留言。
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
Android 繼承DialogFragment彈出dialog對話方塊二