本文主要是实现菜单中的列表选项,具体可以应用到分享一类
Layout布局
itm_view.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" /> </LinearLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.alertdialog.MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <ListView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/list_view" > </ListView>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/ic_launcher"/> </LinearLayout>
main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" tools:context="com.example.alertdialog.MainActivity" > <item android:id="@+id/listView" android:orderInCategory="95" android:showAsAction="never" android:title="自定义ListView"/> <item android:id="@+id/myview" android:orderInCategory="96" android:showAsAction="never" android:title="自定义"/> <item android:id="@+id/mutilistDialog" android:orderInCategory="97" android:showAsAction="never" android:title="多选列表选项"/> <item android:id="@+id/singlelistDialog" android:orderInCategory="98" android:showAsAction="never" android:title="单选列表选项"/> <item android:id="@+id/listDialog" android:orderInCategory="99" android:showAsAction="never" android:title="列表选项"/> <item android:id="@+id/action_settings" android:orderInCategory="100" android:showAsAction="never" android:title="@string/action_settings"/> </menu>
package com.example.alertdialog; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } private void myTask(final String s){ AlertDialog dialog=new AlertDialog.Builder(this).create(); dialog.setTitle(s); dialog.setMessage("确定这样的操作吗?"); dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定: "+s, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(), "已经分享到"+s, Toast.LENGTH_SHORT).show(); } }); dialog.show(); } private class MyAdapter extends ArrayAdapter{ private LayoutInflater infalter; public MyAdapter(Context context, int resource) { super(context, resource); infalter=LayoutInflater.from(context); } @Override public View getView(int position, View convertView, ViewGroup parent) { if(convertView==null) convertView=infalter.inflate(R.layout.item_view, null); TextView text=(TextView) convertView.findViewById(R.id.text); text.setText("data "+position); return convertView; } @Override public int getCount(){ return 50; } } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if(id==R.id.listView){ AlertDialog.Builder builder=new AlertDialog.Builder(this); LayoutInflater mLayoutInflater= getLayoutInflater(); View view=mLayoutInflater.inflate(R.layout.listview, null); ListView lv=(ListView) view.findViewById(R.id.list_view); // String[] data=new String[50]; // for(int i=0;i<data.length;i++){ // data[i]="test: "+i; // } //ArrayAdapter adapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,data); MyAdapter adapter=new MyAdapter(this,-1); lv.setAdapter(adapter); lv.setOnItemClickListener(new AdapterView.OnItemClickListener (){ @Override public void onItemClick(AdapterView<?> arg0, View arg1,int pos, long id) { Toast.makeText(getApplicationContext(), "分享成功!"+pos , Toast.LENGTH_SHORT).show(); }}); lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener (){ @Override public boolean onItemLongClick(AdapterView<?> arg0, View arg1,int pos, long arg3) { Toast.makeText(getApplicationContext(), "长按 -> 分享成功!"+pos , Toast.LENGTH_SHORT).show(); return true; }}); builder.setView(view); builder.setTitle("分享"); builder.create().show(); } if(id==R.id.myview){ AlertDialog.Builder builder=new AlertDialog.Builder(this); LayoutInflater mLayoutInflater= getLayoutInflater(); View view=mLayoutInflater.inflate(R.layout.myview, null); builder.setView(view); builder.create().show(); } if(id==R.id.mutilistDialog){ final String[] items={"同时转发到微信","同时转发到QQ好友","同时转发到新浪微博"}; AlertDialog.Builder builder=new AlertDialog.Builder(this); final boolean[] choices={false,false,true}; builder.setTitle("多选"); builder.setMultiChoiceItems(items, choices, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { //if(isChecked) // Toast.makeText(getApplicationContext(), "选中"+items[which], Toast.LENGTH_SHORT).show(); } }); AlertDialog dialog=builder.create(); dialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String s=""; for(int i=0;i<items.length;i++){ if(choices[i]) s=s+","+items[i]; } // for(int i=0;i<choices.length;i++){ // if(choices[i]){ // s=s+","+items[i]; // } // } Toast.makeText(getApplicationContext(), "选中"+s, Toast.LENGTH_LONG).show(); } }); dialog.show(); } if(id==R.id.singlelistDialog){ final String[] items={"转发到微信","转发到QQ好友","转发到新浪微博"}; AlertDialog.Builder builder=new AlertDialog.Builder(this); builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(), items[which], Toast.LENGTH_SHORT).show(); dialog.dismiss(); } }); AlertDialog dialog=builder.create(); dialog.show(); } if(id==R.id.listDialog){ final String[] items={"转发到微信","转发到QQ好友","转发到新浪微博"}; AlertDialog.Builder builder=new AlertDialog.Builder(this); builder.setItems(items, new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which) { //Toast.makeText(getApplicationContext(), items[which], Toast.LENGTH_SHORT).show(); //处理你们的逻辑 myTask(items[which]); }}); AlertDialog dialog=builder.setTitle("biaoti 1").create(); dialog.show(); } if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }