總網頁瀏覽量

關於我自己

我的相片
人生的必修課是接受無常,人生的選修課是放下執著。

2014年6月8日 星期日

[Android] 建立共用的DialogFragment








InfoDialog dialog = new InfoDialog(context);
Bundle args = new Bundle();
String fromprize = "prize1";
args.putString(InfoDialog.BUNDLE_KEY_PRIZE, fromprize); 
dialog.setArguments(args);
dialog.show();

InfoDialog.java

package com.tsots.PieFee;

import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.view.View.OnClickListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class InfoDialog extends DialogFragment{
 Context context;
 public static final String BUNDLE_KEY_PRIZE = "bundle_key_prize";
 Dialog dialog;
 Button closeButton;
 TextView prizeName, prizeContent;
 String prizeNum = "";
 
 public InfoDialog(Context context) {
  this.context = context;
 }

 @Override
 public void onStop() {
  super.onStop();
 }

 public void setArguments(Bundle args) {
  LayoutInflater inflater = LayoutInflater.from(context);
  View dialogview = inflater.inflate(R.layout.layout_infodialog, null);
  dialog = new Dialog(context, R.style.InfoDialog);
  dialog.setContentView(dialogview);
  
  Bundle arguments = args;
  if (arguments.containsKey(BUNDLE_KEY_PRIZE)) {
   prizeNum = arguments.getString(BUNDLE_KEY_PRIZE);
   initViewComponet(dialog, prizeNum);
  }  
 }
 
 private void initViewComponet(final Dialog dialog, String prizeNum) {
  
  closeButton = (Button) dialog.findViewById(R.id.closeButton);
  closeButton.setOnClickListener(closeButtonOnClickListener);  
  prizeName = (TextView) dialog.findViewById(R.id.prizeName);
  prizeContent = (TextView) dialog.findViewById(R.id.prizeContent);
    
  if(prizeNum.equals("prize1")){
   prizeName.setText(R.string.str_bt_firstprize);
   prizeContent.setText(R.string.str_tv_firstprize);
  }else if(prizeNum.equals("prize2")){
   prizeName.setText(R.string.str_bt_secondprize);
   prizeContent.setText(R.string.str_tv_secondprize);
  }else if(prizeNum.equals("prize3")){
   prizeName.setText(R.string.str_bt_collectprize);
   prizeContent.setText(R.string.str_tv_collectprize);
  }
 }
 
 OnClickListener closeButtonOnClickListener = new OnClickListener() {
  @Override
  public void onClick(View v) {
   dismiss();
  }
 };

 public void show() {
  dialog.show();
 }

 @Override
 public void dismiss() {
  if(dialog!=null){
   dialog.dismiss();
  }
 }
}


沒有留言:

張貼留言