PopupWindow的用法

package com.example.androidtest;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.ViewGroup.LayoutParams;
import android.view.inputmethod.InputMethodManager;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;

public class MainActivity extends FragmentActivity
{

 private View parent;

 @Override
 protected void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
  parent = getLayoutInflater().inflate(R.layout.activity_main, null);
  setContentView(parent);
  final Button button1 = (Button) findViewById(R.id.button1);
  button1.setOnClickListener(new OnClickListener()
  {

   @Override
   public void onClick(View v)
   {

    ListView contentView=new ListView(MainActivity.this);
    String[] objects=new String[8];
    for (int i = 0; i < objects.length; i++)
    {
     objects[i]="hello how do you do !!!"+i;
    }
    contentView.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, objects));
    PopupWindow popupWindow = new PopupWindow(contentView, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, true);
    //点击外部隐藏
    popupWindow.setOutsideTouchable(true);
    //点击外部隐藏........不能少,少了不起作用.........
    popupWindow.setBackgroundDrawable(new BitmapDrawable());
    popupWindow.setAnimationStyle(R.style.popwin_anim_style);
                popupWindow.showAsDropDown(button1);
   }
  });
  iniBt2();
  iniBt3();
 }

 private void iniBt3() {
  findViewById(R.id.button3).setOnClickListener(new OnClickListener() {
   
   private PopupWindow popupWindow;

   @Override
   public void onClick(View v) {


    ListView contentView=new ListView(MainActivity.this);
    String[] objects=new String[8];
    for (int i = 0; i < objects.length; i++)
    {
     objects[i]="hello how do you do !!!"+i;
    }
    contentView.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, objects));
    contentView.setFocusableInTouchMode(true);
    contentView.setOnKeyListener(new OnKeyListener() {
     
     @Override
     public boolean onKey(View v, int keyCode, KeyEvent event) {
      if(event.getAction()==KeyEvent.ACTION_DOWN && keyCode==KeyEvent.KEYCODE_BACK){
       popupWindow.dismiss();
       
      }
      return true;
     }
    });
     popupWindow = new PopupWindow(contentView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, true);
    //点击外部隐藏
    //popupWindow.setOutsideTouchable(true);
    //点击外部隐藏........不能少,少了不起作用.........
    //popupWindow.setBackgroundDrawable(new BitmapDrawable());
    popupWindow.setAnimationStyle(R.style.popwin_anim_style);
                popupWindow.showAtLocation(parent, Gravity.TOP, 0, 0);
   
    
   }
  });
 }

 private void iniBt2()
 {
       findViewById(R.id.button2).setOnClickListener(new OnClickListener()
  {
   
   @Override
   public void onClick(View v)
   {
     View contentView=getLayoutInflater().inflate(R.layout.submit_message, null);
    PopupWindow popupWindow = new PopupWindow(contentView, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, true);
    //点击外部隐藏............
    popupWindow.setOutsideTouchable(true);
    //点击外部隐藏............
    popupWindow.setBackgroundDrawable(new BitmapDrawable());
    //软键盘不会挡着popupwindow
    popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
    popupWindow.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
    //显示键盘
    InputMethodManager systemService = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    systemService.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS); 

   }
  }); 
 }

}

 

R.layout.activity_main:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />

        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />

    </LinearLayout>

</LinearLayout>

 

R.layout.submit_message:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="subMit" />

</LinearLayout>

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值