點擊按鈕彈框顯示ListView,點擊ListView某一項作出相應

最近在作上面描述的功能,網上找了個幾個例子,拼拼湊湊整出來了效果,首先多謝http://www.cnblogs.com/miya2012/archive/2012/08/23/2652224.html,  代碼實現差很少,特意作了個例子。附在最後html

很少說,先看效果圖哈.java

     

好了,上代碼android

MainActivity.javaide

public class MainActivity extends Activity {
	private Button btn;
	private List<String> list = new ArrayList<String>();
	private AlertDialog.Builder builder;
	private AlertDialog alertDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		
		list = initData();
		
		btn = (Button) findViewById(R.id.btn_list);
		
		btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				ShowDialog();//彈框操做
			}
		});
	}
	//初始化數據
	private ArrayList<String> initData() {
           ArrayList<String> list = new ArrayList<String>();
           for(int i = 0;i < 15;i++){
        	String name = "布丁布丁"+i;
            list.add(name);
           }
          return list;
	}
	public void ShowDialog() {
	     Context context = MainActivity.this;
             LayoutInflater inflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
             View layout = inflater.inflate(R.layout.formcommonlist, null);
             ListView myListView = (ListView) layout.findViewById(R.id.formcustomspinner_list);
	     MyAdapter adapter = new MyAdapter(context, list);
             myListView.setAdapter(adapter);
             myListView.setOnItemClickListener(new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int positon, long id) {
		    //在這裏面就是執行點擊後要進行的操做,這裏只是作一個顯示
		   Toast.makeText(MainActivity.this, "您點擊的是"+list.get(positon).toString(), 0).show();
		   if (alertDialog != null) {
			alertDialog.dismiss();
		   }
		}
             });
             builder = new AlertDialog.Builder(context);
             builder.setView(layout);
             alertDialog = builder.create();
             alertDialog.show();
	}
    //自定義的適配器
   class MyAdapter extends BaseAdapter {
	private List<String> mlist;
	private Context mContext;

	public MyAdapter(Context context, List<String> list) {
		this.mContext = context;
		mlist = new ArrayList<String>();
		this.mlist = list;
	}

	@Override
	public int getCount() {
		return mlist.size();
	}

	@Override
	public Object getItem(int position) {
		return mlist.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Person person = null;
		if (convertView == null) {
		    LayoutInflater inflater = LayoutInflater.from(mContext);
		    convertView = inflater.inflate(R.layout.rtu_item,null);
	            person = new Person();
	            person.name = (TextView)convertView.findViewById(R.id.tv_name);
	            convertView.setTag(person);
			}else{
				person = (Person)convertView.getTag();
			}
			person.name.setText(list.get(position).toString());
			return convertView;
		}
	class Person{
	    TextView name;
	}
   }
}

主界面ui

activity_main.xmlthis

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    tools:context="com.example.dialogspinner.MainActivity" >

	 <RelativeLayout
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:background="@drawable/title_background">
          
	    <Button
            android:id="@+id/btn_list"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="15dip"
            android:background="@drawable/commom_title2"
            android:gravity="left|center_vertical"
            android:maxLines="1"
            android:visibility="visible" />
            
	    	<TextView
		        android:id="@+id/tvDeviceName2"
		        android:layout_width="fill_parent"
		        android:layout_height="25dp"
		        android:gravity="center"
		        android:layout_toRightOf="@+id/btn_list"
              	android:text="彈框ListView"
              	android:textSize="17sp"
		        android:textColor="#FF8C00" />
    </RelativeLayout>
</LinearLayout>

彈框顯示樣式spa

formcommonlist.xml.net

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:id="@+id/layout_myview"
    android:layout_height="fill_parent"
    android:background="@android:color/background_light"
    android:orientation="vertical"
    android:padding="0.0dip" >

    <LinearLayout
        android:id="@+id/title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#defafd"
        android:padding="10.0dip" >

        <TextView
            android:id="@+id/label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/title"
            android:text="請選擇"
            android:textColor="#0182af"
            android:textSize="20.0dip" />
    </LinearLayout>

    <ListView
        android:id="@+id/formcustomspinner_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:cacheColorHint="@null"
        android:divider="@android:drawable/divider_horizontal_bright"
        android:scrollbars="none" /> <!-- listview 的線爲灰色 -->

</LinearLayout>

彈框裏的ListView單個選項  code

rtu_item.xmlorm

<?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="35dip"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/tv_name"
        android:layout_width="fill_parent"
        android:layout_height="35dip"
        android:gravity="center"
        android:text="布丁"
        android:textColor="#000000"
        android:textSize="15sp" />

</LinearLayout>

好了,這樣就完成了,有須要源碼的請點擊

下載源碼

相關文章
相關標籤/搜索