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>