Android 如何通过触摸窗外来关闭弹出窗口?
我想在Android 如何通过触摸窗外来关闭弹出窗口?,android,android-activity,popupwindow,dismiss,Android,Android Activity,Popupwindow,Dismiss,我想在弹出窗口的外部单击以关闭该窗口。经过一项研究,我尝试了很多案例,但没有一个对我有效,我能从你那里得到任何帮助吗 这是我的代码: package com.javacodegeeks.android.fragmentstest; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.
弹出窗口的外部单击以关闭该窗口。经过一项研究,我尝试了很多案例,但没有一个对我有效,我能从你那里得到任何帮助吗
这是我的代码:
package com.javacodegeeks.android.fragmentstest;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.annotation.SuppressLint;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.DialogInterface;
@SuppressLint("InflateParams")
public class MainActivity extends Activity implements OnClickListener {
ImageView mButton1;
Context contex;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton1 = (ImageView) findViewById(R.id.button1);
mButton1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
mButton1.setEnabled(false);
LayoutInflater layoutInflater
= (LayoutInflater)getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popupform, null);
final PopupWindow popupWindow = new PopupWindow(
popupView,
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
Button btnbutton1 = (Button)popupView.findViewById(R.id.login);
btnbutton1.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
popupWindow.dismiss();
mButton1 .setEnabled(true);
}
});
popupWindow.showAsDropDown(btnbutton1, 50, 250);
}
});
}
@Override
public void onBackPressed() {
new AlertDialog.Builder(this)
.setTitle("The door is open")
.setMessage("Are you sure you want to leave?")
.setPositiveButton("Leave", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("Stay", null)
.show();
}
尝试在弹出窗口上设置setBackgroundDrawable,如果你在弹出窗口外触摸,它会关闭窗口
比如,
popup.setBackgroundDrawable(new BitmapDrawable(getResources(), ""));
试试这个
mButton1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
mButton1.setEnabled(false);
View popupView = getLayoutInflater().inflate(R.layout.popupform,
null);
final PopupWindow popupWindow= new PopupWindow(popupView,
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT, true);
popupWindow.setBackgroundDrawable(new ColorDrawable(
android.R.color.transparent));
popupWindow.setFocusable(true);
popupWindow.setOutsideTouchable(true);
Button btnbutton1 = (Button)popupView.findViewById(R.id.login);
btnbutton1.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
popupWindow.dismiss();
mButton1 .setEnabled(true);
}
});
popupWindow.showAsDropDown(btnbutton1, 50, 250);
}
});
您可以在onCreate()中使用此选项:
设置以下两个属性:
myPopupWindow.setBackgroundDrawable(new BitmapDrawable());
myPopupWindow.setOutsideTouchable(true);
或者:
myPopupWindow.setFocusable(true);
popupWindow.setOutsideTouchable(真);可能重复:我应该如何使用这个???它给了我代码中的错误。你能把它合并到我的代码里吗?等等。。我会这样做:)LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT,true);这里仍然给出错误,不是可变的!哦,抱歉@PeteCharalampopoulos忘了用popupView替换视图。。现在退房
myPopupWindow.setFocusable(true);