大神们,快来看看这个神奇的问题吧!!
我在Activity弹出一个PopupWindow,PopupWindow上有一个EditText,如果我不去触碰它,我点击返回键可以把PopupWindow关闭,但是我一旦点了输入框,弹出软键盘,那这时我再怎么点返回键都没用,都关不了PopupWindow。以下是我的代码:
把contentview 那三行删掉 试试
问题应该出在这里:edittext获得焦点后弹出软键盘,软键盘的收起实际消费了返回键事件,而之后的分发都指向了软键盘,popupwindow无法截获该事件,具体如何改我还没有想好,稍后再跟帖。
好了,这个问题正如我上面说的那样,事件被分发到edittext了,为edittext 设置onKeyListener即可,可以参照一下以下代码。
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupWindow;
public class PopUpWindowTestActivity extends Activity {
private Button btnPopup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pop_up_window_test);
btnPopup = (Button) findViewById(R.id.btn_popup);
btnPopup.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showPopupWindow(v);
}
});
}
private MPopupWindow popupWindow = null;
protected void showPopupWindow(View v) {
if (popupWindow == null)
popupWindow = new MPopupWindow(this, 500, 300);
popupWindow.show(v);
}
class MPopupWindow extends PopupWindow {
private final Activity mContext;
private View contentView;
public MPopupWindow(Activity ctx, int width, int height) {
super(width, height);
this.mContext = ctx;
contentView = LayoutInflater.from(mContext).inflate(
R.layout.popup_content, null);
init();
}
private EditText txtTest;
private void init() {
txtTest = (EditText) contentView.findViewById(R.id.editText1);
contentView.setFocusable(true);
contentView.setFocusableInTouchMode(true);
this.setFocusable(true);
this.setOutsideTouchable(true);
this.setContentView(contentView);
this.setInputMethodMode(PopupWindow.INPUT_METHOD_FROM_FOCUSABLE);
this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
txtTest.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK) {
dismiss();
return true;
}
return false;
}
});
}
public void show(View parent) {
this.setBackgroundAplha(0.4f);
this.showAtLocation(parent, Gravity.CENTER, 0, 0);
}
@Override
public void dismiss() {
setBackgroundAplha(1.0f);
super.dismiss();
}
private void setBackgroundAplha(float alpha) {
WindowManager.LayoutParams lp = mContext.getWindow()
.getAttributes();
lp.alpha = alpha;
mContext.getWindow().setAttributes(lp);
}
}
}
EditText获取焦点了,但是你惦记返回键是应该能关闭的
卤煮不要淘气,代码明明没问题。
public class PopwindowTest extends Activity {
Mu popupWindow;
View view;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
popupWindow = new Mu(this, 200, 100);
}
public void show(View v) {
System.out.println("dianji");
popupWindow.showAtLocation(v, Gravity.CENTER, 0, 0);
}
class Mu extends PopupWindow {
Context context;
public Mu(Context ctx, int width, int height) {
super(width, height);
this.context = ctx;
view = LayoutInflater.from(context)
.inflate(R.layout.pop_main, null);
view.setFocusable(true);
view.setFocusableInTouchMode(true);
setContentView(view);
view.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK) {
popupWindow.dismiss();
return true;
}
return false;
}
});
setFocusable(true);
setOutsideTouchable(true);
setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
}
}
}
activity_main.xml
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.cn.animtest.MainActivity" >
<Button
android:id="@+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="show"
android:text="@string/hello_world" />
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
pop_main.xml
<?xml version="1.0" encoding="utf-8"?>
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="vertical" >
<EditText
android:id="@+id/edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/abc_search_url_text_normal"
android:text="haha" />
卤煮不要淘气,代码明明没问题。
public class PopwindowTest extends Activity {
Mu popupWindow;
View view;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
popupWindow = new Mu(this, 200, 100);
}
public void show(View v) {
System.out.println("dianji");
popupWindow.showAtLocation(v, Gravity.CENTER, 0, 0);
}
class Mu extends PopupWindow {
Context context;
public Mu(Context ctx, int width, int height) {
super(width, height);
this.context = ctx;
view = LayoutInflater.from(context)
.inflate(R.layout.pop_main, null);
view.setFocusable(true);
view.setFocusableInTouchMode(true);
setContentView(view);
view.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK) {
popupWindow.dismiss();
return true;
}
return false;
}
});
setFocusable(true);
setOutsideTouchable(true);
setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
}
}
}
activity_main.xml
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.cn.animtest.MainActivity" >
<Button
android:id="@+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="show"
android:text="@string/hello_world" />
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
pop_main.xml
<?xml version="1.0" encoding="utf-8"?>
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="vertical" >
<EditText
android:id="@+id/edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/abc_search_url_text_normal"
android:text="haha" />