• 官方关于Dialog的介绍


    将Activity显示成Dialog的形式:

    Tip: If you want a custom dialog, you can instead display an Activity as a dialog instead of using the Dialog APIs. Simply create an activity and set its theme to Theme.Holo.Dialog in the <activity> manifest element:

    <activityandroid:theme="@android:style/Theme.Holo.Dialog">

    That's it. The activity now displays in a dialog window instead of fullscreen.

    将dialog的按钮点击事件传递给Activity:

    package leihu.dialogtoactivity;
    
    import android.annotation.SuppressLint;
    import android.app.DialogFragment;
    import android.os.Bundle;
    import android.support.v4.app.FragmentActivity;
    
    public class MainActivity extends FragmentActivity  implements NoticeDialogFragment.NoticeDialogListener{
    	
    	@Override
    	protected void onCreate(Bundle savedInstaceState) {
    		super.onCreate(savedInstaceState);
    		setContentView(R.layout.activity_main);
    		showNoticeDialog();
    	}
    
    	@SuppressLint("NewApi")
    	public void showNoticeDialog(){
    		DialogFragment dialog = new NoticeDialogFragment();
    		dialog.show(getFragmentManager(), "NoticeDialogFragment");
    		
    	}
    	
    	@Override
    	public void onDialogPositiveClick(DialogFragment dialog) {
    		// TODO Auto-generated method stub
    		System.out.println( "Positive button is pressed");
    	}
    
    	@Override
    	public void onDialogNegativeClick(DialogFragment dialog) {
    		// TODO Auto-generated method stub
    		System.out.println("Negative button is pressed");
    	}
    
    
    }
    

      

    package leihu.dialogtoactivity;
    
    import android.annotation.SuppressLint;
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.app.Dialog;
    import android.app.DialogFragment;
    import android.content.DialogInterface;
    import android.os.Bundle;
    
    @SuppressLint("NewApi")
    public class NoticeDialogFragment extends DialogFragment{
    	
    	public interface NoticeDialogListener{
    		public void onDialogPositiveClick(DialogFragment dialog);
    		@SuppressLint("NewApi")
    		public void onDialogNegativeClick(DialogFragment dialog);
    	}
    	
    	NoticeDialogListener mListener;
    
    	@SuppressLint("NewApi")
    	@Override
    	public void onAttach(Activity activity) {    //Called when a fragment is first attached to its activity                                                     //onCreate(Bundle) will be called after this.
    		super.onAttach(activity);
    		try {
    			mListener = (NoticeDialogListener) activity;
    		} catch (Exception e) {
    			throw new ClassCastException(activity.toString()
    					+ " must implement NoticeDialogListener");
    		}
    	}
    
    	@SuppressLint("NewApi")
    	@Override
    	public Dialog onCreateDialog(Bundle savedInstanceState) {
    		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    		builder.setMessage("fire missiles")
    			.setPositiveButton("frie", new DialogInterface.OnClickListener() {
    				
    				@Override
    				public void onClick(DialogInterface dialog, int which) {
    					mListener.onDialogPositiveClick(NoticeDialogFragment.this);
    				}
    			})
    			 .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
    				
    				@Override
    				public void onClick(DialogInterface dialog, int which) {
    					mListener.onDialogNegativeClick(NoticeDialogFragment.this);
    				}
    			});
    		return builder.create();
    	}
    	
    	
    	
    	
    }
    

      

  • 相关阅读:
    找出水王
    第九周进度表
    [设计模式]组合模式
    [设计模式]外观模式
    [设计模式]策略模式
    [设计模式] 6个设计遵循基本原则
    [OSGI]Eclipse4.2 OSGI依赖Bundle
    [xfire]使用xfire开发webservice的简单示例
    [HTML5 Canvas学习]使用颜色和透明度
    [HTML5 Canvas学习]绘制矩形
  • 原文地址:https://www.cnblogs.com/leihupqrst/p/3718964.html
Copyright © 2020-2023  润新知