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();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值