Android 应用程序首次启动时弹出窗口

发布于 2024-11-05 00:12:44 字数 2068 浏览 0 评论 0原文

我试图找到一个代码,可以在已安装的应用程序首次启动时弹出窗口。就像越来越多的应用程序中开始出现的变更日志一样。 我发现了一些类似的代码,但作为一个初学者,我无法弄清楚将代码到底放在哪里,而且我总是有大量的错误,一旦我尝试修复它们,这些错误仍然不起作用。 我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

绝影如岚 2024-11-12 00:12:44

使用我的此代码,您可以在活动初始​​时间的任何类型的视图上显示弹出窗口。

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

public class PopupDispaly extends Activity {
    private PopupWindow outComePopup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tiwter_login);
        final View anyView = findViewById(R.id.popo_up); // define heade your view
        anyView.postDelayed(new Runnable() {
            @Override
            public void run() {
                callFirstToolTip(anyView);
            }
        }, 5000); // hear you can put your delay time like 5000 mls

    }
    public void callFirstToolTip(View view) {
        Log.i("Started Info","popup");
        View layout = LayoutInflater.from(PopupDispaly.this).inflate(R.layout.popup_copy_delete, null); // define hear your layout file id.
        LinearLayout layCopyDelete = (LinearLayout) layout.findViewById(R.id.layCopyDelete);// hear define your id of sub lay like LinearLayout.
        outComePopup = new PopupWindow(layout);
        outComePopup.setFocusable(true);
        layCopyDelete.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        outComePopup.setWidth(layCopyDelete.getMeasuredWidth());
        outComePopup.setHeight(layCopyDelete.getMeasuredHeight());
        outComePopup.setBackgroundDrawable(getResources().getDrawable(android.R.color.transparent));
        outComePopup.setOutsideTouchable(true);
        outComePopup.showAsDropDown(view);
    }

}

和我的 Custome View 布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/got_it_bg"
    android:layout_gravity="center_horizontal"
   android:id="@+id/layCopyDelete"
    android:layout_marginLeft="100dp"
    android:orientation="horizontal">

</LinearLayout>

Using My this code you can show popup on any type of view on intial time of activity.

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

public class PopupDispaly extends Activity {
    private PopupWindow outComePopup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tiwter_login);
        final View anyView = findViewById(R.id.popo_up); // define heade your view
        anyView.postDelayed(new Runnable() {
            @Override
            public void run() {
                callFirstToolTip(anyView);
            }
        }, 5000); // hear you can put your delay time like 5000 mls

    }
    public void callFirstToolTip(View view) {
        Log.i("Started Info","popup");
        View layout = LayoutInflater.from(PopupDispaly.this).inflate(R.layout.popup_copy_delete, null); // define hear your layout file id.
        LinearLayout layCopyDelete = (LinearLayout) layout.findViewById(R.id.layCopyDelete);// hear define your id of sub lay like LinearLayout.
        outComePopup = new PopupWindow(layout);
        outComePopup.setFocusable(true);
        layCopyDelete.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        outComePopup.setWidth(layCopyDelete.getMeasuredWidth());
        outComePopup.setHeight(layCopyDelete.getMeasuredHeight());
        outComePopup.setBackgroundDrawable(getResources().getDrawable(android.R.color.transparent));
        outComePopup.setOutsideTouchable(true);
        outComePopup.showAsDropDown(view);
    }

}

And my Custome View layout file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/got_it_bg"
    android:layout_gravity="center_horizontal"
   android:id="@+id/layCopyDelete"
    android:layout_marginLeft="100dp"
    android:orientation="horizontal">

</LinearLayout>
白云悠悠 2024-11-12 00:12:44

听起来您应该使用自定义对话框。 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.

静谧 2024-11-12 00:12:44

延迟过程中的PopupWindow.showAsDropDown()可以解决这个问题,可以延迟0.5s执行onCreate()方法中的show popupWindow

PopupWindow.showAsDropDown() in a delayed process can solve this problem, you can execute the show popupWindow in onCreate() method by 0.5s delay.

爱给你人给你 2024-11-12 00:12:44

您应该使用 PopupWindow.showAtLocation 但使用 Activity 视图之一的 post 方法。您还可以使用 findViewById(android.R.id.content) 来使用 Activity 的根视图。
您的代码应该如下所示:

View anyView = findViewById(R.id.anyView);
anyView.post(new Runnable()
{
    @Override
    public void run()
    {
        // Create and show PopupWindow
    }
});

You should use PopupWindow.showAtLocation but use the post method of the one of the views on your activity. you can also use the root view of the activity using findViewById(android.R.id.content).
You code should look like this:

View anyView = findViewById(R.id.anyView);
anyView.post(new Runnable()
{
    @Override
    public void run()
    {
        // Create and show PopupWindow
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文