显示消息的逻辑:我有一个包含100个字符串的String数组存储在其中。我随机从该数组中选择一个字符串,并填充弹出窗口的TextView以显示该消息。请建议是否有比我已经在这里做的更好的方法。还有可能逻辑是,如果选择了一条消息,则在显示其他消息至少一次之前不会选择同一消息吗?
显示弹出窗口的逻辑:这是我无法实现的部分。我不想将弹出窗口锚定到任何用户事件或按钮单击上。我只是想在一段时间后显示消息 - 比如Thread.sleep(3000);。现在我已经尝试使用PopupWindow来实现这一点,使用以下代码。
PopupWindow infoPopup;
LinearLayout infoLayout;
TextView infoTextView;
Button infoButton;
infoTextView = new TextView(this);
infoTextView.setText("Testing Popup Text");
infoTextView.setPadding(10,10,10,10);
infoButton = new Button(this);
infoButton.setText("Close");
infoLayout = new LinearLayout(this);
infoLayout.setOrientation(LinearLayout.VERTICAL);
infoLayout.addView(infoTextView);
infoLayout.addView(infoButton);
infoPopup = new PopupWindow(infoLayout,LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
infoPopup.setContentView(infoLayout);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
infoPopup.showAtLocation((CoordinatorLayout)findViewById(R.id.main_content),Gravity.CENTER,100,100);
但是这个弹出窗口在最后一行显示错误,给出了我的空指针:
(CoordinatorLayout)findViewById(R.id.main_content)
参数。
我遇到的问题有:
首先,我不确定这是否是显示自定义UI弹出窗口的正确方法。我知道
AlertDialog
,但不确定在这种情况下哪个选项是最好的-请建议。为什么
CoordinatorLayout
会显示空指针?如何在此弹出窗口中实现右上角(X)按钮的逻辑?