今天查资源练习了这个的使用,对于焦点问题,整的脑袋有点大,不知道是模拟器原因 还是 程序原因,在弹出窗口里德按钮点击效果总是不明显。有时弹出窗口显示时,返回键感觉不好使,退出整个程序。晕了。不过收益还是挺大的了,学会使用了。有的时候alertdialog给他setView也是可行的,在android ophone 完全开发讲义 上那个悬浮的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 stubSystem.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);    }@Overridepublic boolean onTouchEvent(MotionEvent event) {//在popwindow出现时不触发,他也是有焦点的,//popwindow不设置setOutsideTouchable(false),setTouchable(true),setTouchInterceptor,点击popwindow外部,消失// TODO Auto-generated method stubSystem.out.println("TestPopWindow.onTouchEvent");return super.onTouchEvent(event);}Button.OnClickListener ShowWindow=new Button.OnClickListener(){public void onClick(View v) {// TODO Auto-generated method stubshowPopWindow();}    };        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 stubTextView 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 stubbtShowWindow=(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

更多相关文章

  1. Android实现ListView嵌套Checkbox真正的多选、全选、反选
  2. Android中TextView首行缩进处理办法
  3. Android免费获取短信验证码
  4. 【Android】Source Insight 基本用法 ( 导入 Android(安卓)源码
  5. Android——详解Paint的setPathEffect(PathEffect effect)
  6. Android入门教程(八)之-----简单的Button事件响应综合提示控件To
  7. android 实现滤镜效果
  8. Android常用复杂控件使用(四)--Fragment(续)
  9. Android如何一步步实现状态栏一体化效果

随机推荐

  1. LeakCanary2.3 核心原理浅析
  2. Android学习(一)(初学)SharedPreferences
  3. Unity与Android交互通信
  4. android studio中android.support.v7.wid
  5. 扫描已匹配的蓝牙设备
  6. Android(安卓)Studio使用aidl实现进程间
  7. Android中的Rect类各参数的意义
  8. 有趣的动画视图集合:Android(安卓)View An
  9. Android(安卓)sdk tool地址及相关工具
  10. Android(安卓)ListView上下滑动与item左