【问题标题】:How do I create a floating window using onClick attribute in Android?如何在 Android 中使用 onClick 属性创建浮动窗口?
【发布时间】:2014-07-02 16:26:31
【问题描述】:

所以我正在开发一个 Android 应用程序(在 Eclipse 中)并且我碰壁了。在我的应用程序中,我有一个抽屉滑出选项列表。我希望用户能够单击其中一个选项并打开一个带有表单的浮动窗口。我正在尝试使用按钮上的 onClick 属性而不是使用 onClickListener 来执行此操作。这是否可能无需使用 onClickListener 或者我是否试图避免不可避免的情况?我的布局中按钮的 onClick 属性的值为“newWindow”。

我的 MainActivity 类

public void newWindow(View v){
    Intent intent = new Intent(){
        LayoutInflater layoutInflater 
        = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);  
       View popupView = layoutInflater.inflate(R.layout.popup, null);  
                final PopupWindow popupWindow = new PopupWindow(
                  popupView, 
                  LayoutParams.WRAP_CONTENT,  
                        LayoutParams.WRAP_CONTENT); 

    };
    startActivity(intent);

    }

我看到我的 popupView 变量未使用,但我不确定该放在哪里,或者我是否朝着正确的方向前进。提前感谢您的帮助!

【问题讨论】:

  • 我能想到的最简单的选项是“对话”。无论是用户输入还是通知都可以解决问题。然后转到片段。不过,开始这样的活动看起来并不漂亮。

标签: java android xml eclipse android-intent


【解决方案1】:

我一生都无法弄清楚您为什么要在这里尝试使用 Intent 启动 Activity。您的代码应为:

LayoutInflater layoutInflater = (LayoutInflater)getBaseContext()
        .getSystemService(LAYOUT_INFLATER_SERVICE);  
View popupView = layoutInflater.inflate(R.layout.popup, null);  
final PopupWindow popupWindow = new PopupWindow(popupView,
        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
popupWindow.showAtLocation(...); // or showAsDropdown(...)

【讨论】:

  • 谢谢,我在 Android 开发方面完全是绿色的。我只是在模仿。到目前为止,我还没有完全了解 Intent 的工作原理。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-25
  • 1970-01-01
  • 2010-10-20
  • 2016-06-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多