显示来自其他类的PopUpWindow-Android
基本上,我有两门课: -MyActivity.java -OtherClass.java显示来自其他类的PopUpWindow-Android,android,onclick,popupwindow,Android,Onclick,Popupwindow,基本上,我有两门课: -MyActivity.java -OtherClass.java @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); context = getApplicationContext() ; main
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context = getApplicationContext() ;
main_activity = this ;
layout = (LinearLayout) findViewById(R.id.layout);
/*
* Do lot of stuff
*/
}
MyActivity.java概述:
那里没有什么有趣的东西。。。除了otherClass.java所需的实例之外
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context = getApplicationContext() ;
main_activity = this ;
layout = (LinearLayout) findViewById(R.id.layout);
/*
* Do lot of stuff
*/
}
OtherClasse.java概述:
它有一个可点击的文本视图。当我做一个长点击事件时,我想显示一个弹出窗口(在UI线程上,所以我的活动…)
日志表明我在onLongClick()中输入了。。。
但是应用程序崩溃了
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
但是MyActivity.layout是一个静态线性布局,所以我可以向它添加视图。。。
关于如何从其他类的onClickListener显示PopUpWindow有什么建议吗
编辑:
@Override
public boolean onLongClick(View v) {
PopupWindow popup = new PopupWindow(BlaActivity.context);
TextView tv = new TextView(BlaActivity.context);
LayoutParams para = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(para);
tv.setText("My future text ...");
popup.setContentView(tv);
popup.setWidth(400);
popup.setHeight(180);
popup.showAtLocation(tv, Gravity.CENTER_HORIZONTAL, 10, 10);
popup.update();
return true;
}
返回一个
android.view.WindowManager$BadTokenException:无法添加窗口--标记null无效;你的活动正在进行吗
因为
popup.showAtLocation(电视,重心水平,10,10)代码>在电视上调用公共IBinder getWindowToken()
。。。Witch显然没有令牌…您在这里所指的视图
popup.setContentView( view );
这可能是问题所在。每次都创建一个弹出窗口的新实例,但如果每次都使用相同的textview,则这就是导致IllegalStateException的原因
下面的代码只是一个活动和第二个类。onLongClick创建另一个类的指令并调用showPopUp
另一个类的构造函数将上下文作为参数,稍后用于实例化弹出窗口
showPopUp接收一个视图,该视图用作弹出窗口的父视图
活动的onCreate
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button)findViewById(R.id.button1);
final RelativeLayout parent = (RelativeLayout)findViewById(R.id.layout);
b.setOnLongClickListener( new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
new AnotherClass(getApplicationContext()).showPopUp(parent);
return true;
}
});
}
二等舱
要在OtherClass中使用click/touch侦听器,您可以像通常一样声明它们,但要在侦听器中创建弹出窗口,您需要提供活动的上下文。像这样的东西很好
public class AnotherClass {
Context ctx;
public AnotherClass(Context ctx){
this.ctx = ctx;
//***EXAMPLE*** wont actually be visible as its not added to a view
Button b2 = new Button(ctx);
b2.setText("show popup");
b2.setOnLongClickListener( new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
showPopUp(v);//View v can be used as the parent
return true;
}
});
}
public void showPopUp(View parent) {
PopupWindow popup = new PopupWindow(ctx);
TextView tv = new TextView(ctx);
LayoutParams para = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(para);
tv.setText("My future text ...");
popup.setContentView(tv);
popup.setWidth(400);
popup.setHeight(180);
popup.showAtLocation(parent, Gravity.CENTER_HORIZONTAL, 10, 10);
popup.update();
}
}
和xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:background="@android:color/black" android:id="@+id/layout">
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content" android:id="@+id/button1"
android:text="Button" android:layout_alignParentTop="true"
android:layout_alignParentLeft="true" android:layout_marginLeft="51dp"
android:layout_marginTop="28dp"></Button>
</RelativeLayout>
那么您有什么建议来解决这个问题?甚至使用onLongClick(view v)popup.setContentView(v)中的视图代码>。。。我还有那个非法国家,事实上除外。我只是想在弹出窗口上显示一些东西。。。我还尝试了一个刚刚创建的文本视图,它不起作用。。。查看添加的代码。在更新的代码中,您将textview tv作为showAtLocation中的父项传递,但tv已经是弹出窗口的子项,我将使用适用于meWell的代码编辑我的答案。。。很好用,谢谢。但事实上,它并不是完全相同的架构。在代码中,Main活动具有LongClickListener。但我的架构如下。。。OtherClass包含textView和我需要的其他属性。OtherClass的多个实例是在解析文档后创建的(因此,我在解析的XML文档中定义了每个单词一个OtherClass实例…),然后MyActivity只显示OtherClass实例的几个文本视图。那么是否可以在OtherClass中使用onLongClick?并且仍然在UI线程中显示弹出窗口?我应该在MyActivity中定义一个静态LongClickListener,并让其他类的textView监听它吗?