Android 应用程序首次启动时弹出窗口
我试图找到一个代码,可以在已安装的应用程序首次启动时弹出窗口。就像越来越多的应用程序中开始出现的变更日志一样。 我发现了一些类似的代码,但作为一个初学者,我无法弄清楚将代码到底放在哪里,而且我总是有大量的错误,一旦我尝试修复它们,这些错误仍然不起作用。 我正在 Eclipse 中处理一个 android 项目,并且使用 webview 来显示网站。
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_height="fill_parent" android:layout_width="fill_parent" android:scrollbarAlwaysDrawVerticalTrack="false"/>
</LinearLayout>
Java 文件:
package com.A2Ddesigners.WhatThe;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.view.KeyEvent;
public class Whatthe extends Activity {
WebView webview;
/** Called when the activity is first created. */
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
webview.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.webview);
webview.setWebViewClient(new HelloWebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.setInitialScale(50);
webview.getSettings().setUseWideViewPort(true);
webview.loadUrl("http://mdsitest2.com/");
}
private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
I am trying to find a code that will do a popup at the initial start up on an installed app. Much like a changelog that is starting to appear in more and more apps.
I have found some similar codes, but being a beginner I haven't been able to figure out where to exactly put the code in and I always have tons of errors that still do not work once I try and fix them.
I am working in Eclipse with an android project, and I'm using a webview to show a website.
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_height="fill_parent" android:layout_width="fill_parent" android:scrollbarAlwaysDrawVerticalTrack="false"/>
</LinearLayout>
Java File:
package com.A2Ddesigners.WhatThe;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.view.KeyEvent;
public class Whatthe extends Activity {
WebView webview;
/** Called when the activity is first created. */
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
webview.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.webview);
webview.setWebViewClient(new HelloWebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.setInitialScale(50);
webview.getSettings().setUseWideViewPort(true);
webview.loadUrl("http://mdsitest2.com/");
}
private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用我的此代码,您可以在活动初始时间的任何类型的视图上显示弹出窗口。
和我的 Custome View 布局文件
Using My this code you can show popup on any type of view on intial time of activity.
And my Custome View layout file
听起来您应该使用自定义对话框。 http://developer.android.com/guide/topics/ui/dialogs.html
页面底部有一个代码示例。要显示它,您可以在主活动的 onCreate() 方法中调用 onCreateDialog(int) 。
Sounds like you should use a custom dialog box. http://developer.android.com/guide/topics/ui/dialogs.html
There's a code example at the bottom of the page. To show it, you can call onCreateDialog(int) from inside your main activity in its onCreate() method.
延迟过程中的
PopupWindow.showAsDropDown()
可以解决这个问题,可以延迟0.5s执行onCreate()
方法中的showpopupWindow
。PopupWindow.showAsDropDown()
in a delayed process can solve this problem, you can execute the showpopupWindow
inonCreate()
method by 0.5s delay.您应该使用
PopupWindow.showAtLocation
但使用 Activity 视图之一的post
方法。您还可以使用findViewById(android.R.id.content)
来使用 Activity 的根视图。您的代码应该如下所示:
You should use
PopupWindow.showAtLocation
but use thepost
method of the one of the views on your activity. you can also use the root view of the activity usingfindViewById(android.R.id.content)
.You code should look like this: