How to create your custom Dialog


Google provider two avaiable Dialog - AlertDialog & ProgressDialog.

1. AlertDialog example
public class AlertDialogUsage extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        new AlertDialog.Builder(AlertDialogUsage.this)
        .setTitle("Powe level")
        .setMessage("Griffin's AlertDialog sample!")
        .setPositiveButton("Yes",
           new DialogInterface.OnClickListener()
            {
           public void onClick(DialogInterface dialoginterface, int i)
             {
        	   Log.d("TAG","[setPositiveButton]");
             }
             }
            )
         .setNegativeButton("No", new DialogInterface.OnClickListener()
            {
           public void onClick(DialogInterface dialoginterface, int i)
             {
        	   Log.d("TAG","[setNegativeButton]");
             }
             }
            )

        .show();
        
    }
}



2. ProgressDialog example
public class ProgressDialogUsage extends Activity {
	private final static int NOTIFICATION_CALCULATION_FINISH = 0;
	
	private TextView tv;
	private ProgressDialog pd;

	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        tv = (TextView) this.findViewById(R.id.text);
        tv.setText("Press any key to start calculation");

    }
    
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    	CalculationWorkingInitialize();
    	
    	return super.onKeyDown(keyCode, event);
    }

    public void  CalculationWorkingInitialize() {
    	//1. to start ProgressDialog
    	pd = ProgressDialog.show(this, "calculation", "please waiting...", true,
    			false);
    	pd.setIcon(R.drawable.icon);
    	
    	//2. to start an Thread to do calculation working
    	CalculationWorkingBody myWork = new CalculationWorkingBody();
    	Thread c_thread = new Thread(myWork);
    	//3. to call start() to do CalculationWorking working
    	c_thread.start();

    }
    
    private class CalculationWorkingBody implements Runnable {
    	
		@Override
		public void run() {
			
			// TODO Auto-generated method stub
			//4. the working,from 1000 -> 0
			LongWorkingBody();
			
			//5. to call sendEmptyMessage() to notificate this working is done
			progressHandler.sendEmptyMessage(NOTIFICATION_CALCULATION_FINISH);
		}
    	
    }
    
    //to count from 1000 to 0
    private void LongWorkingBody(){
    	long i = 10000;
    	while(i>0){
			//Log.d("TAG",String.valueOf(i));
			i = i-1;
		}
    	
    	
    }
    
    //Define the Handler that receives messages from the thread calculation
	private Handler progressHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch(msg.arg1){
			case NOTIFICATION_CALCULATION_FINISH:
				pd.dismiss();
				tv.setText("calculation End!");
				break;
				
			}
		}
	};

}




Also, to custom the dialog as following is advise.
public class CustomDialog extends Dialog {

	public CustomDialog(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}
	
	 protected void onCreate(Bundle savedInstanceState){
		 super.onCreate(savedInstanceState);
		 
		 setContentView(R.layout.custom_dialog);
		 setTitle("Custom Dialog");

		 TextView text = (TextView)findViewById(R.id.text);
		 text.setText("Hello, this is a custom dialog!");
		 ImageView image = (ImageView)findViewById(R.id.image);
		 image.setImageResource(R.drawable.sepurple);
		 
		 findViewById(R.id.button_yes).setOnClickListener(new Button.OnClickListener(){
				public void onClick(View v) {
					// TODO Auto-generated method stub
					dismiss();
					
				}
	        });
		 
		 findViewById(R.id.button_no).setOnClickListener(new Button.OnClickListener(){
				public void onClick(View v) {
					// TODO Auto-generated method stub
					dismiss();
					
				}
	        });
	 }
	 
	 //called when this dialog is dismissed
	 protected void onStop() {
	 }
	 
}



And above code is so simple that no more words to discuss.

To list how to do:
1. to defines an xml file to layout the customize dialog. i.g. //custom_dialog.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/layout_root"
              android:orientation="horizontal"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:padding="10dp"
              >
    <ImageView android:id="@+id/image"
               android:layout_width="wrap_content"
               android:layout_height="fill_parent"
               android:layout_marginRight="10dp"
               />
    <LinearLayout 
        android:orientation="vertical"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:padding="5px" 
        >
    <TextView android:id="@+id/text"
              android:layout_width="wrap_content"
              android:layout_height="fill_parent"
              android:textColor="#FFF"
              />
    <LinearLayout 
        android:orientation="horizontal"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:padding="5px" 
        >
    <Button android:id="@+id/button_yes"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text=" Yes "
              android:gravity="center"
              />
    <Button android:id="@+id/button_no"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text=" No "
              android:gravity="center"
              />
    </LinearLayout>
    </LinearLayout>
</LinearLayout>


Above containes an ImageView, TextView and two Button.

2. to inflate the xml.
setContentView(R.layout.custom_dialog);


3. to initia the value of each resource.
TextView text = (TextView)findViewById(R.id.text);
		 text.setText("Hello, this is a custom dialog!");
		 ImageView image = (ImageView)findViewById(R.id.image);
		 image.setImageResource(R.drawable.sepurple);



3. to define Button 's OnClickListener ...


That's all!

How to initial the dialog.
cd.show();


How to dismiss it.
cd.dismiss();

你可能感兴趣的:(java,thread,c,xml,Google)