【问题标题】:How to navigate to next activity after user clicks on AlertDialog OK button?用户单击 AlertDialog OK 按钮后如何导航到下一个活动?
【发布时间】:2014-02-05 21:01:23
【问题描述】:

很抱歉在http://stackoverflow.com 搜索后提出这个问题。但是在不同部分使用以下这些代码时,我无法处理下一个活动。

在这里,当用户未连接互联网时,弹出窗口会提醒并在 alertdialog 中显示“您未连接到互联网”,当用户点击 alertdilog 中的 退出 按钮时,应用程序将关闭。 如果用户有互联网连接,弹出窗口会提醒并在警报对话框中显示“您有互联网连接”,当用户点击继续按钮时,应用程序会继续下一个活动。 我在继续下一个活动时遇到问题,该怎么做?

下面的代码显示在他们的部分

AndroidDetectInternetConnectionActivity.java中的代码

package com.example.detectinternetconnection;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class AndroidDetectInternetConnectionActivity extends Activity {

 // flag for Internet connection status
Boolean isInternetPresent = false;

    // Connection detector class
ConnectionDetector cd;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

 Button btnStatus = (Button) findViewById(R.id.btn_check);

 // creating connection detector class instance
  cd = new ConnectionDetector(getApplicationContext());

    /**
     * Check Internet status button click event
     * */
    btnStatus.setOnClickListener(new View.OnClickListener() {


        @Override
        public void onClick(View v) {

            // get Internet status
        isInternetPresent = cd.isConnectingToInternet();

            // check for Internet status
        if (isInternetPresent) {
                // Internet Connection is Present
                // make HTTP requests
        AlertDialog.Builder builder = new AlertDialog.Builder(AndroidDetectInternetConnectionActivity.this);
        builder.setMessage("You have network connection.")
            .setTitle("Internet Connection")
            .setCancelable(false);
    AlertDialog alert = builder.create();
                    alert.setButton("Continue", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which){ 
                          // Do call some activity. Do what you    wish to;
startActivity(new Intent(AndroidDetectInternetConnectionActivity.this,MainActivity2.class));
                           }
                          }); 
                    alert.show();
            }

 else {
                // Internet connection is not present
                // Ask user to connect to Internet
      AlertDialog.Builder builder = new AlertDialog.Builder(AndroidDetectInternetConnectionActivity.this);
       builder.setMessage("You need a network connection to use this application. Please turn on mobile network or Wi-Fi in Settings.")
         .setTitle("No Internet Connection")
         .setCancelable(false);
    AlertDialog alert = builder.create();
    alert.setButton2("Exit", new DialogInterface.OnClickListener(){
                    public void onClick(DialogInterface dialog, int which) {
                         //close the program
                    AndroidDetectInternetConnectionActivity.this.finish();
        }
            });
                      // Showing Alert Message
               alert.show();
                 }
        }

ConnectionDetector.java 中的代码 包 com.example.detectinternetconnection;

import android.app.AlertDialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class ConnectionDetector {

private Context _context;

public ConnectionDetector(Context context){
    this._context = context;
}

public boolean isConnectingToInternet(){
    ConnectivityManager connectivity = (ConnectivityManager) _context.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;
}
}

AndroidManifest.xml 中的代码

  <?xml version="1.0" encoding="utf-8"?>
  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.detectinternetconnection"
  android:versionCode="1"
  android:versionName="1.0" >

 <uses-sdk android:minSdkVersion="8" />

 <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".AndroidDetectInternetConnectionActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".MainActivity2"></activity>
  </application>

 <!-- Internet Permissions -->
 <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

 </manifest>

告诉我哪里做错了?

【问题讨论】:

  • 这里我收到错误 AlertDialog alert = builder.create(); alert.setButton("Continue", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which){ // 调用一些活动。做你想做的; startActivity(new Intent(AndroidDetectInternetConnectionActivity.this, MainActivity2.class)); }
  • 你到底遇到了什么问题。
  • 说 Intent 无法解析
  • 你能发布你的日志猫吗...

标签: java android


【解决方案1】:

总的代码没问题,但是最后通过create()创建初始化alertdialog..

    if (isInternetPresent) {              
    // Internet Connection is Present
   // make HTTP requests
    AlertDialog.Builder builder = new   AlertDialog.Builder(AndroidDetectInternetConnectionActivity.this);
    builder.setMessage("You have network connection.")
        .setTitle("Internet Connection")
        .setCancelable(false);

                alert.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which){ 
                      // Do call some activity. Do what you    wish to;
         startActivity(new  Intent(AndroidDetectInternetConnectionActivity.this,MainActivity2.class));
                  }
               }); 
          AlertDialog alert = builder.create();
                alert.show();

编辑

这是您的总活动课程。

package com.example.detectinternetconnection;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class AndroidDetectInternetConnectionActivity  extends Activity implements   OnClickListener {

Boolean isInternetPresent = false;
ConnectionDetector cd;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    Button btnStatus = (Button) findViewById(R.id.btnStatus);
    cd = new ConnectionDetector(getApplicationContext());
    btnStatus.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    isInternetPresent = cd.isConnectingToInternet();
    if (isInternetPresent) {

        AlertDialog alert;
        AlertDialog.Builder builder = new AlertDialog.Builder(
                AndroidDetectInternetConnectionActivity .this);
        builder.setTitle("Internet Connection");
        builder.setCancelable(false);

        builder.setPositiveButton("Continue",
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        startActivity(new Intent(AndroidDetectInternetConnectionActivity .this,
                                MainActivity2 .class));
                    }
                });

        alert = builder.create();
        alert.show();
    }

    else {

        AlertDialog alert;
        AlertDialog.Builder builder = new AlertDialog.Builder(
                AndroidDetectInternetConnectionActivity .this);
        builder.setTitle("No Internet Connection");
        builder.setMessage("Please Connect To Internet");
        builder.setCancelable(false);

        builder.setPositiveButton("Exit",
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });

        alert = builder.create();
        alert.show();
    }
}

}

我想你会照顾其他课程。有关一些知识,你可以参考开发者网站上的Android Dialog 教程。

【讨论】:

  • else { AlertDialog.Builder builder = new AlertDialog.Builder(AndroidDetectInternetConnectionActivity.this); builder.setMessage("您需要网络连接才能使用此应用程序。请在设置中打开移动网络或Wi-Fi。") .setTitle("无网络连接") .setCancelable(false); AlertDialog alert = builder.create(); alert.setButton2("Exit", new DialogInterface.**OnClickListener**(){ public void onClick(DialogInterface dialog, int which) { AndroidDetectInternetConnectionActivity.this.finish(); } }); // 显示警报消息 alert.show(); }
  • in first blod error is The type new DialogInterface.OnClickListener(){} must implement the继承的抽象方法DialogInterface.OnClickListener.onClick(DialogInterface, int)
  • 单击 ctrl + shift + O ..正如 Nun Chai 所说...用于自动实施。此错误正在出现,因为您没有导入适当的包。
  • 先生,请将您的电子邮件 ID 发送给我。 Nanu kuda karnataka davanu。
  • 错误已清除,但在运行应用程序期间出现这些错误 [2014-02-04 16:32:13 - AndroidDetectInternetConnection] HOME 在设备 'emulator-5556' 上启动 [2014-02- 04 16:32:13 - AndroidDetectInternetConnection] 将 AndroidDetectInternetConnection.apk 上传到设备 'emulator-5556' [2014-02-04 16:32:14 - AndroidDetectInternetConnection] 安装 AndroidDetectInternetConnection.apk...
【解决方案2】:

将此添加到Activity 的开头:

import android.content.Intent;

【讨论】:

  • 感谢工作.. 但在 AlertDialog 中出现错误 alert = builder.create(); alert.setButton2("Exit", new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog, int which) 说“类型 new DialogInterface.OnClickListener(){} 必须实现继承的抽象方法 DialogInterface.OnClickListener.onClick(DialogInterface , int)"
【解决方案3】:

用途:

Intent mainIntent;

                    mainIntent = new Intent(CurrentClass.this,ClassToBeLoaded.class); 

                    CurrentClass.this.startActivity(mainIntent);
                    CurrentClass.this.finish();

完成后也按Ctrl+Shift+ O

上述过程将所有必需的导入添加到您的类文件中。

【讨论】:

  • 感谢工作。但这里的另一个错误是我得到 AlertDialog alert = builder.create(); alert.setButton2("Exit", new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog, int which) { //关闭程序 AndroidDetectInternetConnectionActivity.this.finish(); } });
  • 错误提示“类型 new DialogInterface.OnClickListener(){} 必须实现继承的抽象方法 DialogInterface.OnClickListener.onClick(DialogInterface, int)”
  • 以上问题请参考 Ranjit Patils 的回答!
  • 另外,如果以上对您有帮助,您可以点击旁边的勾号接受我的回答:)
猜你喜欢
  • 1970-01-01
  • 2016-02-08
  • 1970-01-01
  • 2015-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多