为此,我按照@theLittleNaruto 的建议在评论部分创建了自己的 PopUpWindow。
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_marginTop="80dp"
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Country"
android:layout_gravity="center_vertical|center_horizontal"/>
</LinearLayout>
popup_example.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dip" >
<ListView
android:id="@+id/lstview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
showpopup_1.java
package com.example.spinnerworking;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.Toast;
public class showpopup_1 extends Activity {
boolean click = true ;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final LayoutInflater inflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final Button b = (Button) findViewById(R.id.btn);
final View pview = inflater.inflate(R.layout.popup_example,
(ViewGroup) findViewById(R.layout.main));
final PopupWindow pw = new PopupWindow(pview);
Log.i("hello", "hello") ;
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (click) {
// if onclick written here, it gives null pointer exception.
// if onclick is written here it gives runtime exception.
pw.showAtLocation(v, Gravity.CENTER_HORIZONTAL, 0, 0);
pw.update(8, 0, 150, 200);
String[] array = new String[] { "tushar", "pandey",
"almora" };
ListView lst = (ListView) pview.findViewById(R.id.lstview);
adapterHello adapter = new adapterHello(showpopup_1.this);
lst.setAdapter(adapter);
lst.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Toast.makeText(showpopup_1.this, "pandey",
Toast.LENGTH_SHORT).show();
}
});
click = false ;
}
else
{
pw.dismiss();
click = true;
}
}
});
}
}
class adapterHello extends BaseAdapter {
String array[] = new String[] { "tushar", "pandey", "almora", "hello",
"tushar", "pandey", "almora", "hello", "tushar", "pandey",
"almora", "hello" };
showpopup_1 context;
public adapterHello(showpopup_1 context) {
this.context = context;
}
public int getCount() {
// TODO Auto-generated method stub
return array.length;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
TextView text = new TextView(context);
text.setHeight(30);
text.setPadding(10, 8, 0, 0);
text.setTextColor(Color.BLACK);
text.setText(array[position]);
text.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i("clicked", "tushar");
}
});
return text;
}
}