咖啡动力 发表于 2013-1-14 18:18:51

Android弹出菜单的灵活运用(自定义AlertDialog)

方法一:  
 
     // 软件设置

         TextView hcsz1 = new TextView(
           ComicLivePlayerApp.this);
         hcsz1.setTextSize(25);
         hcsz1.setText("缓冲设置");
 
         TextView zdsz1 = new TextView(
           ComicLivePlayerApp.this);
         zdsz1.setTextSize(25);
         zdsz1.setText("振动设置");
 
         TextView yxsz1 = new TextView(
           ComicLivePlayerApp.this);
         yxsz1.setTextSize(25);
         yxsz1.setText("音效设置");

         String[] hcsz = new String[] { "大", "中","小" };
         String[] zdsz = new String[] { "开", "关" };
         String[] yxsz = new String[] { "开", "关" };
 
         Spinner sphcsz = null;
         Spinner spzdsz = null;
         Spinner spyxsz = null;  //需要用spinner
         ArrayAdapter<String> adapterhcsz;
         ArrayAdapter<String> adapterzdsz;
         ArrayAdapter<String> adapteryxsz;   //需要用arrayAdapter
 
         LinearLayout.LayoutParams LP_FF = new LinearLayout.LayoutParams(
           LayoutParams.FILL_PARENT,
           LayoutParams.WRAP_CONTENT);  //布局
 
         sphcsz = new Spinner(
           ComicLivePlayerApp.this);
         sphcsz.setLayoutParams(LP_FF);  //设置布局
 
         adapterhcsz = new ArrayAdapter<String>(
           ComicLivePlayerApp.this,
           android.R.layout.simple_list_item_1,
           hcsz);  //string数组转换成arrayAdapter
 
         sphcsz.setAdapter(adapterhcsz);  //arrayAdapter添加到spinner
//spinner设置监听
         sphcsz.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
          @Override
          public void onItemSelected(
            AdapterView<?> arg0, View arg1,
            int arg2, long arg3) {
           // TODO Auto-generated method stub
           hcsz_set = arg2;
          }
          @Override
          public void onNothingSelected(
            AdapterView<?> arg0) {
           // TODO Auto-generated method stub
          }
         });
//监听完毕
 
//第二个string数组
         spzdsz = new Spinner(
           ComicLivePlayerApp.this);
         spzdsz.setLayoutParams(LP_FF);
         adapterzdsz = new ArrayAdapter<String>(
           ComicLivePlayerApp.this,
           android.R.layout.simple_list_item_1,
           zdsz);
         spzdsz.setAdapter(adapterzdsz);
         spzdsz.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
          @Override
          public void onItemSelected(
            AdapterView<?> arg0, View arg1,
            int arg2, long arg3) {
           // TODO Auto-generated method stub
           zdsz_set = arg2;
          }
          @Override
          public void onNothingSelected(
            AdapterView<?> arg0) {
          }
         });
 
//第三个string数组
         spyxsz = new Spinner(
           ComicLivePlayerApp.this);
         spyxsz.setLayoutParams(LP_FF);
         adapteryxsz = new ArrayAdapter<String>(
           ComicLivePlayerApp.this,
           android.R.layout.simple_list_item_1,
           yxsz);
         spyxsz.setAdapter(adapteryxsz);
         spyxsz.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
          @Override
          public void onItemSelected(
            AdapterView<?> arg0, View arg1,
            int arg2, long arg3) {
           // TODO Auto-generated method stub
           yxsz_set = arg2;
          }
          @Override
          public void onNothingSelected(
            AdapterView<?> arg0) {
           // TODO Auto-generated method stub
          }
         });
 
 
//把软件设置所有项都添加到布局中
         final LinearLayout lyset1 = new LinearLayout(
           ComicLivePlayerApp.this);

         lyset1.setOrientation(LinearLayout.HORIZONTAL);
         LinearLayout lyset2 = new LinearLayout(
           ComicLivePlayerApp.this);

         lyset2.setOrientation(LinearLayout.HORIZONTAL);
         LinearLayout lyset3 = new LinearLayout(
           ComicLivePlayerApp.this);

         lyset3.setOrientation(LinearLayout.HORIZONTAL);
         LinearLayout lyset5 = new LinearLayout(
           ComicLivePlayerApp.this);

         lyset5.setOrientation(LinearLayout.VERTICAL);
         lyset1.addView(hcsz1);
         lyset1.addView(sphcsz);
         lyset2.addView(zdsz1);
         lyset2.addView(spzdsz);
         lyset3.addView(yxsz1);
         lyset3.addView(spyxsz);
         //
         lyset5.addView(lyset1);
         lyset5.addView(lyset2);
         lyset5.addView(lyset3);
         lyset5.setPadding(10, 10, 0, 0);
 
//将lyset5放入弹出框,弹出
         new AlertDialog.Builder(
           ComicLivePlayerApp.this)
           .setTitle("软件设置")
           .setView(lyset5)    //把布局设进去
           .setPositiveButton(
             "确定",
             new DialogInterface.OnClickListener() {
              @Override
              public void onClick(
                DialogInterface arg0,
                int arg1) {
               com.index.comicliveSA.Data date = new com.index.comicliveSA.Data();
               date.cacheSetup = (byte) hcsz_set;// 缓冲设置
 
               switch (zdsz_set) { // 振动设置
               case 0: // 开
                Data.playVibrate = true;
                break;
               case 1: // 关
                Data.playVibrate = false;
                break;
               }
 
               switch (yxsz_set) { // 振动设置
               case 0: // 开
                Data.playVibrate = true;
                break;
               case 1: // 关
                Data.playVibrate = false;
                break;
               }
 
               date.refreshSetup = (byte) sysz_set;// 刷页设置
 
              }
             })
           .setNegativeButton(
             "取消",
             new DialogInterface.OnClickListener() {
              @Override
              public void onClick(
                DialogInterface arg0,
                int arg1) {
           
               setContentView(fl);
              }
             }).create().show();
  
        }
       }
      }).show();
   }
 
 
 
方法二:
 
 searchDialog = new AlertDialog.Builder(MainActivityGroup.this).create();
   searchDialog.setView(getLayoutInflater().inflate(R.layout.manhua_searchcartoon, null));
 searchDialog.show();
searchDialog.getWindow().setGravity(Gravity.CENTER);
   searchDialog.getWindow().setLayout(android.view.WindowManager.LayoutParams.FILL_PARENT
    , android.view.WindowManager.LayoutParams.WRAP_CONTENT);
   searchDialog.getWindow().setContentView(getLayoutInflater().inflate(R.layout.manhua_searchcartoon, null));
 
//R.layout.manhua_searchcartoon是定义好的布局
 
如:
 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="20dip">
<LinearLayout
android:orientation="vertical"
android:background="@drawable/gridview_xml_background"
android:padding="15dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<EditText
android:background="@drawable/logininput2"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:hint="请输入搜索关键字" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/radio0"
android:text="按漫画名"
android:textSize="18dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true" />
<RadioButton
android:id="@+id/radio1"
android:text="按作者名"
android:textSize="18dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="搜索"
android:textColor="#ffffff"
android:textSize="16dip"
android:id="@+id/button_search"
android:background="@drawable/cartoon_select_button"
android:layout_alignParentLeft="true" />
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="取消"
android:textColor="#ffffff"
android:textSize="16dip"
android:background="@drawable/cartoon_select_button"
android:layout_alignParentRight="true"
android:id="@+id/button_return" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
 
 
方法三:  
 
 searchDialog = new AlertDialog.Builder(MainActivityGroup.this).create();
searchDialog .settitle("test");
searchDialog .setMessage("testmessage");
 searchDialog.show();
页: [1]
查看完整版本: Android弹出菜单的灵活运用(自定义AlertDialog)