自定义Dialog UI
程序员文章站
2022-06-14 15:21:37
...
本例中CustomDialog继承Dialog,使用custom_dialog.xml布局文件。
custom_dialog.xml,这里就随便布局了一下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:padding="10dp" >
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="10dp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5px" >
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#FFF" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5px" >
<Button
android:id="@+id/button_yes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text=" Yes " />
<Button
android:id="@+id/button_no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text=" No " />
</LinearLayout>
</LinearLayout>
</LinearLayout>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/main_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="click to start an Dialog"
/>
</LinearLayout>
CustomDialog.java,CustomDialog继承Dialog,使用起来和Activity差不多,通过setContentView()指定使用的布局文件,剩下的就和Activity差不多了,就是一些findViewById()和setListener()。
package com.android.CustomDialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
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);
Button buttonYes = (Button) findViewById(R.id.button_yes);
buttonYes.setHeight(5);
buttonYes.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
dismiss();
}
});
Button buttonNo = (Button) findViewById(R.id.button_no);
buttonNo.setSingleLine(true);
buttonNo.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
dismiss();
}
});
}
//called when this dialog is dismissed
protected void onStop() {
Log.d("TAG","+++++++++++++++++++++++++++");
}
}
CustomDialogUsage.java,这里重写了Activity的onCreateDialog()方法创建Dialog(),并为Dialog设置了OnDismissListener,没什么特别的。
package com.android.CustomDialog;
import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class CustomDialogUsage extends Activity {
OnDismissListener lis;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lis = new OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),
((CustomDialog)dialog).toString(),
Toast.LENGTH_SHORT)
.show();
}
};
Button buttonYes = (Button) findViewById(R.id.main_button);
buttonYes.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
showDialog(0);
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
Dialog d = new CustomDialog(this);
d.setOnDismissListener(lis);
return d;
}
@Override
protected void onPrepareDialog(int id, Dialog dialog) {
// TODO Auto-generated method stub
super.onPrepareDialog(id, dialog);
}
}