/* 自定义的dialog */
public class QuitDialog extends Activity implements OnClickListener {
private Button bt_true , bt_false ;
private TextView content ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
requestWindowFeature(Window. FEATURE_NO_TITLE );
setContentView(R.layout. dialog_b1 );
bt_true = (Button) findViewById(R.id. dialog_b1_true );
bt_false = (Button) findViewById(R.id. dialog_b1_false );
content = (TextView) findViewById(R.id. toast_content );
content .setGravity(Gravity. LEFT );
content .setPadding(15, 10, 0, 0);
bt_true .setOnClickListener(QuitDialog. this );
bt_false .setOnClickListener(QuitDialog. this );
content .setText( " 您确定要退出吗? " );
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id. dialog_b1_true :
final ActivityManager am = (ActivityManager) QuitDialog. this
.getSystemService(Context. ACTIVITY_SERVICE );
am.restartPackage(QuitDialog. this .getPackageName());
break ;
case R.id. dialog_b1_false :
finish();
break ;
}
}
}
/* 自定义dialog xml */
<? xml version = "1.0" encoding = "utf-8" ?>
< LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
android:id = "@+id/toast_layout_root" android:orientation = "vertical"
android:layout_width = "240dip" android:layout_height = "170dip" >
< LinearLayout android:layout_width = "180dip"
android:layout_height = "50dip" android:paddingLeft = "10dp" android:orientation = "horizontal" >
< ImageView android:id = "@+id/toast_img" android:background = "@drawable/icon0"
android:layout_width = "wrap_content" android:layout_height = "wrap_content"
android:layout_marginRight = "10dp" />
< TextView android:id = "@+id/toast_title" android:text = " 温馨提示 "
android:layout_width = "wrap_content" android:layout_height = "wrap_content"
android:layout_gravity = "center_vertical" android:textSize = "22px"
android:textColor = "#FF34B3" />
</ LinearLayout >
< ImageView android:src = "@drawable/line_2"
android:layout_width = "wrap_content" android:layout_height = "wrap_content"
android:layout_gravity = "center_horizontal" />
< TextView android:id = "@+id/toast_content"
android:layout_width = "wrap_content" android:layout_height = "wrap_content"
android:layout_marginTop = "10dp" android:textSize = "20px"
android:textColor = "#ffffff" android:layout_gravity = "center" />
< LinearLayout android:layout_width = "fill_parent"
android:layout_height = "wrap_content" android:layout_marginTop = "10dp" android:background = "#949494"
android:orientation = "vertical" >
< LinearLayout android:layout_width = "wrap_content"
android:layout_height = "fill_parent"
android:orientation = "horizontal" >
< Button android:id = "@+id/dialog_b1_true" android:text = " 确定 "
android:textColor = "#5b5b5b" android:textSize = "20px" android:gravity = "center"
android:layout_width = "100dip" android:layout_height = "wrap_content" android:layout_margin = "10dp"
android:background = "@drawable/button_selector_3" ></ Button >
< Button android:id = "@+id/dialog_b1_false" android:text = " 取消 "
android:textColor = "#5b5b5b" android:textSize = "20px" android:gravity = "center"
android:layout_width = "100dip" android:layout_height = "wrap_content"
android:background = "@drawable/button_selector_3" android:layout_margin = "10dp" ></ Button >
</ LinearLayout >
</ LinearLayout >
</ LinearLayout >
AndroidManifest.xml 声明
< activity android:name = "QuitDialog" android:theme = "@android:style/Theme.Dialog" ></ activity >
/* 在主程序中的调用 */
Intent intent = new Intent();
intent.setClass( this , QuitDialog. class );
startActivity(intent);