`
阅读更多

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>

 

 

 

 

 

分享到:
评论

相关推荐

    Android入门之PopupWindow用法实例解析

    主要介绍了Android入门之PopupWindow用法,对于Android初学者来说有一定的学习借鉴价值,需要的朋友可以参考下

    Android中PopupWindow使用方法详解

    主要为大家详细介绍了Android中PopupWindow使用方法,感兴趣的小伙伴们可以参考一下

    PopupWindow用法实例

    PopupWindow详细用法实例源代码

    PopupWindow的基本使用

    该项目演示了,PopupWindow的基本使用,相关PopupWindow代码有独立的Class文件,并且有大量的注释说明。实现的功能有弹出的PopupWindow上有EditText能与软键盘并存不冲突,弹出PopupWindow的时候背景自动改变灰度为...

    Android PopupWindow用法解析

    主要为大家详细解析了Android PopupWindow用法,对PopupWindow源码进行分析,感兴趣的小伙伴们可以参考一下

    Android PopupWindow使用方法小结

    前几天要用到PopupWindow,一时竟想不起来怎么用,赶紧上网查了查,自己写了个demo,并在此记录一下PopupWindow的用法。 使用场景 PopupWindow,顾名思义,就是弹窗,在很多场景下都可以见到它。例如ActionBar/...

    android popupwindow用法详解

    本文实例为大家分享了android popupwindow的用法,供大家参考,具体内容如下 一、基本用法 一般做法,新建类继承popupwindow。例 /** * popupwindow基本用法 * Created by Administrator on 2015/11/25. */ ...

    Android组件popupwindow使用方法详解

    主要为大家详细介绍了Android组件popupwindow使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

    Android开发中PopupWindow用法实例分析

    主要介绍了Android开发中PopupWindow用法,结合实例形式分析了PopupWindow弹出窗口效果的使用技巧,需要的朋友可以参考下

    popupWindow使用

    popupWindow使用的方法,以及PopupWindow样式的使用

    PopupWindow使用方法详解

    主要为大家详细介绍了PopupWindow的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

    popupwindow的Demo

    popupwindow的基本用法

Global site tag (gtag.js) - Google Analytics