// 将Activity显示为Dialog的方法: // 在AndroidManifest.xml文件中, 设置Activity的属性 // android:theme="@android:style/Theme.Dialog" import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.content.DialogInterface.OnMultiChoiceClickListener; import android.view.LayoutInflater; import android.view.Menu; import android.widget.ImageView; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle(R.string.app_name); builder.setIcon(R.drawable.ic_launcher); builder.setMessage("Message"); CharSequence[] items = {"item 0", "item 1", "item 2"}; boolean[] checkedItems = {true, true, true, true}; // 选项 // builder.setItems(items, null); // 单选框 // builder.setSingleChoiceItems(items, 0, new OnClickListener() { // @Override // public void onClick(DialogInterface arg0, int arg1) { // // TODO Auto-generated method stub // Toast.makeText(MainActivity.this, "" +arg1, Toast.LENGTH_SHORT).show(); // } // }); // 复选框 // builder.setMultiChoiceItems(items, checkedItems, new OnMultiChoiceClickListener() { // @Override // public void onClick(DialogInterface arg0, int arg1, boolean arg2) { // // TODO Auto-generated method stub // Toast.makeText(MainActivity.this, "" +arg1 + ", " + arg2, Toast.LENGTH_SHORT).show(); // } // }); builder.setView(getLayoutInflater().inflate(R.layout.activity_main, null)); // 按钮索引 // Positive: -1 // Negative: -2 // Neutral: -3 builder.setPositiveButton("确定", null); builder.setNegativeButton("取消", null); builder.setNeutralButton("中立", null); builder.create(); builder.show(); // Dialog dialog = new Dialog(this); // dialog.setContentView(R.layout.activity_main); // dialog.show(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
时间: 2024-11-08 10:29:09