当前位置: 首页 > 知识库问答 >
问题:

类似trip advisor的对话框,并在android中显示自定义背景或启动屏幕

隆睿
2023-03-14

我是Android开发初学者。下面我给我的主要activity.java代码。我的要求,如果互联网连接或wifi不工作在移动设备上,然后以下事情执行

  1. 如果互联网连接不起作用,则在应用程序启动时显示“我的闪屏”,并给出对话框或警告,如trip advisor的屏幕截图检查此图像和屏幕截图。如果按“取消”按钮,则对话框消失,仅显示启动屏幕,如果按“重试”,则尝试执行“连接到internet”

在我的代码中,我使用代码购买对话框,我不想购买功能这不是我的要求。我想要和旅行顾问一模一样。对我来说很简单,如果有人编辑我的代码,并显示在哪里进行更改非常感谢提前

package com.example.edarabia;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;


@SuppressLint("SetJavaScriptEnabled")

public class MainActivity extends Activity{
WebView mywebview;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
     mywebview = (WebView) findViewById(R.id.webview);
    if(isNetworkConnected() == true){
    mywebview.getSettings().setJavaScriptEnabled(true);
    mywebview.setWebViewClient(new myWebClient());
        mywebview.loadUrl("http://www.grafdom.com/operations/projects/ma/edarabiaapp/");        
    mywebview.getSettings().setBuiltInZoomControls(true);
    mywebview.getSettings().setLoadWithOverviewMode(false);
    mywebview.getSettings().setUseWideViewPort(false);
    }else{
    showBuyDialog();
    }
}






//  @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
    // Check if the key event was the Back button and if there's history
//        if ((keyCode == KeyEvent.KEYCODE_BACK) && mywebview.canGoBack()) {
//          mywebview.goBack();
//            return true;
 //       }

 //       return super.onKeyDown(keyCode, event);

//  }

 // To handle "Back" key press event for WebView to go back to previous screen.
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    if ((keyCode == KeyEvent.KEYCODE_BACK) && mywebview.canGoBack()) {
        mywebview.goBack();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

@Override
public void onBackPressed() {
    finish();
}


public class myWebClient extends WebViewClient
{
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
        view.loadUrl(url);
        return true;
    }
}

//// This method will retun boolean value if net conect then value will be true otherwise false
private boolean isNetworkConnected() {
    ConnectivityManager connectivity = (ConnectivityManager)     getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity != null) {
        NetworkInfo[] info = connectivity.getAllNetworkInfo();
        if (info != null)
            for (int i = 0; i < info.length; i++)
                if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }
    }
    return false;
}

public void showBuyDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    builder.setTitle("just for testing");

    builder.setMessage("Check you net conectivity....");
    builder.setCancelable(false);
    builder.setPositiveButton("Buy", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            Intent browserIntent = new Intent(
                    Intent.ACTION_VIEW,
                    Uri.parse("https://www.google.com"));
            startActivity(browserIntent);
        }
    });
    builder.show();
}




}

共有3个答案

越欣怡
2023-03-14

尝试替换show ButDialog()方法,

public void showBuyDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    builder.setTitle("Unable to connect");   
    builder.setMessage("You must have an Internet connection to use TripAdvisor. Please connect and try again.");
    builder.setCancelable(false);

    builder.setPositiveButton("Try again", new DialogInterface.OnClickListener() {
         @Override
         public void onClick(DialogInterface dialog, int id) {
               isNetworkConnected();
         }
    });
    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    });
    builder.show();
}

要获得与图中相同的UI,请在应用程序标记之前添加以下行,如下所示:,

<uses-sdk android:targetSdkVersion="15" />

这种对话框可从Android操作系统3.0(蜂蜜梳)及以上版本获得。因此,您无法在以前的版本中看到对话框。

卢宜然
2023-03-14

我得到了解决方案和下面的更新代码

package com.example.edarabia;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;


@SuppressLint("SetJavaScriptEnabled")

public class MainActivity extends Activity{
WebView mywebview;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if(isNetworkConnected() == true){
    setContentView(R.layout.activity_main);
    mywebview = (WebView) findViewById(R.id.webview);       
    mywebview.getSettings().setJavaScriptEnabled(true);
    mywebview.setWebViewClient(new myWebClient());
    mywebview.loadUrl("http://www.grafdom.com/operations/projects/ma/edarabiaapp/");        
    mywebview.getSettings().setBuiltInZoomControls(true);
    mywebview.getSettings().setLoadWithOverviewMode(false);
    mywebview.getSettings().setUseWideViewPort(false);
    }else{
    setContentView(R.layout.splash);    
    showBuyDialog();
    }
}






//  @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // Check if the key event was the Back button and if there's history
//        if ((keyCode == KeyEvent.KEYCODE_BACK) && mywebview.canGoBack()) {
//          mywebview.goBack();
//            return true;
 //       }

 //       return super.onKeyDown(keyCode, event);

//  }

 // To handle "Back" key press event for WebView to go back to previous screen.
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    if ((keyCode == KeyEvent.KEYCODE_BACK) && mywebview.canGoBack()) {
        mywebview.goBack();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

@Override
public void onBackPressed() {
    finish();
}


public class myWebClient extends WebViewClient
{
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
        view.loadUrl(url);
        return true;
    }
}

//// This method will retun boolean value if net conect then value will be true otherwise false
private boolean isNetworkConnected() {
    ConnectivityManager connectivity = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity != null) {
        NetworkInfo[] info = connectivity.getAllNetworkInfo();
        if (info != null)
            for (int i = 0; i < info.length; i++)
                if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }
    }
    return false;
}

public void showBuyDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    builder.setTitle("Unable to connect");

    builder.setMessage("You Must have an Internet connection to use Edarabia. Please connect and try again.");
    builder.setCancelable(false);
    builder.setNegativeButton("Close", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            finish(); 
        }
    });
    builder.show();
}




}
阚小云
2023-03-14

这可能会有帮助:检测Android上是否有可用的互联网连接

或者尝试挖掘BroadcastReceivers。示例代码:

public class InternetConnectionStateReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        _v("Network connectivity change");
        if (intent.getExtras() != null) {
            NetworkInfo ni = (NetworkInfo) intent.getExtras().get(ConnectivityManager.EXTRA_NETWORK_INFO);
            if (ni != null && ni.getState() == NetworkInfo.State.CONNECTED) {
                _v("Network " + ni.getTypeName() + " connected");
                onNetworkConnection(context, true);
            }
        }
        if (intent.getExtras().getBoolean(ConnectivityManager.EXTRA_NO_CONNECTIVITY, Boolean.FALSE)) {
            _v("There's no network connectivity");
            onNetworkConnection(context, false);
        }
    }

    private void onNetworkConnection(Context context, boolean isConnected) {
          //show dialog
    }

}
 类似资料:
  • 我正在尝试弹出一个自定义对话框,当我点击一个按钮,但它不会弹出在所有。我的应用程序基本上是一个日历,我将使用sqlite在日历中添加/保留约会和其他内容,使用对话框,这是指定约会细节的地方。 我为此使用的代码如下: 我做错了什么?

  • 如何删除Android对话框中的黑色背景。这张照片显示了问题所在。

  • 我有一个主要活动,它在列表中显示一些数据,还有一个nfc asynctask,它从卡中读取一些数据。我想实现以下行为: 如果关闭应用程序并在手机附近放置卡,则应启动主要活动,同时启动nfc任务。异步任务的结果应显示在对话框中 我目前的方法总是从主要活动开始。这意味着,有时,我的主要活动有多个实例,当用户点击后退按钮时,会打开另一个活动实例,而不是切换到主菜单。 清单

  • 在我的应用程序中,当我试图显示自定义的框时,它在android手机中运行良好。现在,当我在android选项卡上安装应用程序时,一切都很好,只有自定义框有问题。不显示。所以我想,我应该检查正常对话框,它工作正常。下面是普通对话框和警报对话框的代码。

  • 所需的输出图像: 我已经尝试了代码,我的输出是这样的。请帮助我生产我所显示的输出。 我的代码: 我需要一个白色透明的背景和所需的输出图像显示的对齐。请帮我提些建议。

  • 我的Android应用程序需要显示一个全屏位图作为背景,然后在上面使用OpenGL ES显示一些动态的3D图形(1.1或2.0--尚未决定)。背景图像是同一个应用程序中WebView组件的快照,因此它的尺寸已经完全适合屏幕。 我是OpenGL新手,但我知道显示位图的常规方法包括将其缩放成一个POT纹理(glTexImage2D)、配置矩阵、为矩形创建一些顶点并使用GLDraWarrays来显示。似