android學習雜記(2)--PopWindow下拉對話框的使用

PopWindow是android提供的一種對話框,另一種對話框爲AlertDialog。兩者經常混用,但也有一定的不同。
主要區別在於:

AlertDialog的位置固定,而PopupWindow的位置可以隨意
AlertDialog是非阻塞線程的,而PopupWindow是阻塞線程的
PopupWindow的位置按照有無偏移分,可以分爲偏移和無偏移兩種;按照參照物的不同,可以分爲相對於某個控件(Anchor錨)和相對於父控件。

在篩選方面PopWindow使用的較多一點,而在關於提示用戶是否進行某項操作方面則大多使用dialog.

下面代碼爲本人在某培訓機構學習時老師教的一個演示程序,共享下:

package com.myselfview.aa;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

    private EditText et_number;
    private ImageButton ib_tip;
    private List<String> numbers = new ArrayList<String>();
    private MyAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        et_number = (EditText)findViewById(R.id.et_number);
        ib_tip = (ImageButton)findViewById(R.id.ib_tip);

        ib_tip.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        //PopupWindwo
        ListView lv = new ListView(this);
        lv.setBackgroundResource(R.drawable.listview_background);
        //取消ListView的分割線
        lv.setDividerHeight(0);
        for(int i=0; i<100; i++){
            numbers.add("12345670"+i);
        }
        adapter = new MyAdapter();
        lv.setAdapter(adapter);


    // 創建一個PopupWindow對象,併爲其設置包含的view、寬度以及高度
    final PopupWindow pw = new PopupWindow(lv,et_number.getWidth(), 200);



        pw.setBackgroundDrawable(new BitmapDrawable());

       //PopupWindow是阻塞的,當不設置獲取焦點的時候,只能使用dismiss()方法關閉。
        pw.setFocusable(true);

       //通過該方法設置PopupWindow彈出的位置
        pw.showAsDropDown(et_number, 2, 0);

        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub
                String number = (String) adapter.getItem(position);
                et_number.setText(number);
                et_number.setSelection(number.length());

                //關閉PopupWindow
                pw.dismiss();
            }


        });
    }

    private class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return numbers.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return numbers.get(position);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            View view = getLayoutInflater().inflate(R.layout.activity_list, null);

            ImageView iv_user = (ImageView) view.findViewById(R.id.iv_user);
            TextView tv_number = (TextView) view.findViewById(R.id.tv_number);
            ImageView iv_delete = (ImageView) view.findViewById(R.id.iv_delete);

            iv_user.setImageResource(R.drawable.user);
            tv_number.setText(numbers.get(position));
            iv_delete.setImageResource(R.drawable.delete);

            iv_delete.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    //移除條目對應的數據
                    numbers.remove(position);
                    //刷新ListView
                    adapter.notifyDataSetChanged();
                }
            });
            return view;
        }
    }

}

下拉菜單ListView的佈局界面

<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:gravity="center_vertical"
    tools:context=".MainActivity" >

    <ImageView
        android:id="@+id/iv_user"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/user"/>
    <TextView
        android:id="@+id/tv_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1234556"
        android:layout_marginLeft="3dp"
        android:layout_toRightOf="@id/iv_user"/>
    <ImageView
        android:id="@+id/iv_delete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/delete"
        android:layout_marginLeft="3dp"
        android:layout_toRightOf="@id/tv_number"/>
</RelativeLayout>

主界面代碼

<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"
    tools:context=".MainActivity" >

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <EditText
            android:id="@+id/et_number"
            android:layout_width="200dp"
            android:layout_height="wrap_content"/>
        <ImageButton
            android:id="@+id/ib_tip"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/down_arrow"
            android:layout_alignRight="@id/et_number"
            android:layout_centerVertical="true"/>
    </RelativeLayout>
</RelativeLayout>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章