android之popupwindow问题

lyfree 发布于 2014/06/05 23:12
阅读 347
收藏 0

下面是我写的popupwindow测试代码

MainActivity:


package com.example.transparent;


import com.example.androidui.R;
import com.example.transparent.TransparentMenuTest.ImageAdapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.LinearLayout.LayoutParams;

public class PopWindowTest extends Activity {
	private int[] resArray = new int[] { R.drawable.icon_menu_addto,
			R.drawable.icon_menu_audioinfo, R.drawable.icon_menu_findlrc,
			R.drawable.icon_menu_scan };

	private String[] title = new String[] { "添加歌曲", "歌曲信息", "查找歌词", "搜索歌词" };
	Button btn = null;
	PopupWindow pw = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tempmain);
		btn = (Button) findViewById(R.id.test);
		LayoutInflater inflater = (LayoutInflater) this
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		final View view = inflater.inflate(R.layout.pop_menu, null);
		GridView grid1 = (GridView) view.findViewById(R.id.menuGridChange);

		grid1.setAdapter(new ImageAdapter(this));
		grid1.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				switch (position) {
				case 0:
					Toast.makeText(PopWindowTest.this, "0", 1000).show();
					break;
				case 1:
					Toast.makeText(PopWindowTest.this, "1", 1000).show();
					break;
				case 2:
					Toast.makeText(PopWindowTest.this, "2", 1000).show();
					break;
				case 3:
					Toast.makeText(PopWindowTest.this, "3", 1000).show();
					break;

				default:
					break;
				}
			}
		});
		
		final View myview = getLayoutInflater().inflate(R.layout.poptest, null);
		
		btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//PopupWindow pw = new PopupWindow();
				
				pw = new PopupWindow(view, LayoutParams.FILL_PARENT,
						LayoutParams.WRAP_CONTENT);
				
				// 需要设置一下此参数,点击外边可消失 
				pw.setBackgroundDrawable(new BitmapDrawable()); 
				//pw.setBackgroundDrawable(null); 
				//设置PopupWindow外部区域是否可触摸(设置点击窗口外边窗口消失) 
				pw.setOutsideTouchable(true); 
				// 设置此参数获得焦点,否则无法点击 
				pw.setFocusable(true);
				
				//pw.setOutsideTouchable(false);
				// NND, 第一个参数, 必须找个View
				//pw.showAtLocation(findViewById(R.id.tv), Gravity.CENTER, 0, 300);
				pw.showAtLocation(findViewById(R.id.tv), Gravity.CENTER, 0, 300);
				
				 

			}
		});
	}
	public class ImageAdapter extends BaseAdapter {
		private Context context;

		public ImageAdapter(Context context) {
			this.context = context;
		}

		@Override
		public int getCount() {
			return resArray.length;
		}

		@Override
		public Object getItem(int arg0) {
			return resArray[arg0];
		}

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

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			LinearLayout linear = new LinearLayout(context);

			LinearLayout.LayoutParams params = new LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

			linear.setOrientation(LinearLayout.VERTICAL);

			ImageView iv = new ImageView(context);

			iv.setImageBitmap(((BitmapDrawable) context.getResources()
					.getDrawable(resArray[arg0])).getBitmap());

			LinearLayout.LayoutParams params2 = new LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

			params2.gravity = Gravity.CENTER;

			linear.addView(iv, params2);

			TextView tv = new TextView(context);

			tv.setText(title[arg0]);

			LinearLayout.LayoutParams params3 = new LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

			params3.gravity = Gravity.CENTER;

			linear.addView(tv, params3);
			EditText et = new EditText(context);  //注释1
			linear.addView(et);                   //注释2
			return linear;
		}
	}
}



这是tempmain.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="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    <Button 
        android:id="@+id/test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="打开popupwindow"
        />
    <Button 
        android:id="@+id/t2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="t2main"
        />
</LinearLayout>



这是pop_menu.xml:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
        <GridView
        android:id="@+id/menuGridChange"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@drawable/semitransparent_circular_bead"
        android:columnWidth="60.0dip"
        android:gravity="center"
        
        android:horizontalSpacing="10.0dip"
        android:numColumns="auto_fit"
        android:padding="5.0dip"
        android:stretchMode="columnWidth"
        android:verticalSpacing="3.0dip" />
</LinearLayout>



如果将注释1和注释2注释起来,popupwindow中的四个image可以点击,如果不注释 popupwindow中的 四个image就不可以点击,但是四个edittext可以输入字符。请问这是什么原因?


加载中
0
Line
Line
焦点问题。focusable 设为true 或者 在layout布局中设置 android:descendantFocusability=”blocksDescendants” 
lyfree
lyfree
focusable是设置的true啊
返回顶部
顶部