popupwindow练习

今天查资源练习了这个的使用,对于焦点问题,整的脑袋有点大,不知道是模拟器原因还是程序原因,在弹出窗口里德按钮点击效果总是不明显。有时弹出窗口显示时,返回键感觉不好使,退出整个程序。晕了。不过收益还是挺大的了,学会使用了。有的时候alertdialog给他setView也是可行的,在androidophone完全开发讲义上那个悬浮的activity也很好,设置android:theme属性。

activity的代码:

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.TextView;
import android.widget.Toast;

public class TestPopwindow extends Activity {
    /** Called when the activity is first created. */
	private TextView txChange=null;
	private Button btShowWindow=null;
	private PopupWindow pop=null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        View popview=this.getLayoutInflater().inflate(R.layout.popwindow,null);
		pop=new PopupWindow(popview,ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT,true);
		pop.setAnimationStyle(android.R.anim.slide_in_left);
		pop.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.screen_background_light));
		pop.setOnDismissListener(new OnDismissListener()//消失监听
		{

			public void onDismiss() {
				// TODO Auto-generated method stub
				System.out.println("TestPopWindow.pop.OnDismissListener");
				Toast.makeText(TestPopwindow.this,"悬浮消失",Toast.LENGTH_SHORT).show();
			}
			
		});
//		pop.setTouchable(true);//设置pop是否可以点击,默认是true,为false时点击屏幕响应的是屏幕的,里面的控件无焦点
//		pop.setTouchInterceptor(new OnTouchListener()//pop的touchable设为true,监听发生在pop上的触屏事件,在屏幕上的事件也是可以响应的
//		{
//
//			public boolean onTouch(View v, MotionEvent event) {
//				// TODO Auto-generated method stub
//				System.out.println("TestPopWindow.pop.OnTouchInterceptor");
////				pop.dismiss();
//				return true;
//			}
//			
//		});
		pop.setOutsideTouchable(true);//默认是false,控制pop是否监听pop以外的触屏事件。这个方法在pop可触,并且no focusable时有意义。这意味着在pop外点击屏幕,
        findViews();					//触屏是在后面的activity响应的。在pop内touch无效,在外时 pop dismiss,按钮可点
        btShowWindow.setOnClickListener(ShowWindow);
    }

	@Override
	public boolean onTouchEvent(MotionEvent event) {//在popwindow出现时不触发,他也是有焦点的,
//		popwindow不设置setOutsideTouchable(false),setTouchable(true),setTouchInterceptor,点击popwindow外部,消失
		// TODO Auto-generated method stub
		System.out.println("TestPopWindow.onTouchEvent");
		return super.onTouchEvent(event);
	}

	Button.OnClickListener ShowWindow=new Button.OnClickListener(){

		public void onClick(View v) {
			// TODO Auto-generated method stub
			showPopWindow();
		}
    };
    
    private void showPopWindow() {
		// TODO Auto-generated method stub
    	View popcontentview=pop.getContentView();
    	TextView tvinfor=(TextView) popcontentview.findViewById(R.id.textview_popwindow_showinfo);
    	tvinfor.setText("测试窗口");
    	ImageView iview=(ImageView) popcontentview.findViewById(R.id.imageview_popwindow_showimage);
    	iview.setImageResource(R.drawable.android11);
    	Button btTest=(Button) popcontentview.findViewById(R.id.button_popwindow_showinfo);
    	btTest.requestFocus();//pop set touchable 为true,并且pop 有焦点,显示点击效果
    	pop.setFocusable(true);
    	System.out.println("btTest.hasFocus()========"+btTest.hasFocus());//popupwindow中的按钮无焦点
    	System.out.println("pop.isFocusable()========"+pop.isFocusable());//输出 true
    	
    	System.out.println("TestPopwindow.this.hasWindowFocus()===="+TestPopwindow.this.hasWindowFocus());
		System.out.println("TestPopwindow.this.pop.isFocusable()===="+TestPopwindow.this.pop.isFocusable());
//    	btTest.hasFocus();
    	btTest.setOnClickListener(new  Button.OnClickListener()
    	{

			public void onClick(View v) {
				// TODO Auto-generated method stub
				TextView poptextview=(TextView)pop.getContentView().findViewById(R.id.textview_popwindow_showinfo);
				System.out.println("TestPopwindow.pop.btTest.click.v===="+v.getId());
				poptextview.setText("after click");
				txChange.setText("have changed");
//				pop.dismiss();
			}
    	});
		pop.setWidth(200);//设置这个悬浮的宽高尺寸
		pop.setHeight(300);
		pop.setAnimationStyle(R.style.PopupAnimation);//设置出现和消失的动画
		pop.showAtLocation(findViewById(R.id.linearlayout_main_show),Gravity.CENTER|Gravity.CENTER, 0,0);
	}
	
	private void findViews() {
		// TODO Auto-generated method stub
		btShowWindow=(Button)findViewById(R.id.button_main_showwindow);
		txChange=(TextView)findViewById(R.id.textview_main_showchange);
	}
}

pop布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/linearlayout_popwindow_view"
  android:background="#b0000000"
  android:orientation="vertical"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
  <ImageView
  android:id="@+id/imageview_popwindow_showimage"
  android:src="@drawable/icon"
  android:layout_marginLeft="10dip"
  android:layout_marginTop="30dip"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  />
  <TextView
  android:id="@+id/textview_popwindow_showinfo"
  android:text="information"
  android:textSize="18dip"
  android:layout_marginTop="15dip"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  />
   <Button
  android:id="@+id/button_popwindow_showinfo"
  android:text="buttontest"
  android:textSize="18dip"
  android:layout_marginTop="15dip"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  />
</LinearLayout>

动画效果借助了eoeandroid上的效果http://www.eoeandroid.com/thread-48051-1-1.html

相关推荐