0

我不知道我的代码有什么问题,因为我什么都试过了。我创建了警报对话框,并且我看到的所有网络都向其他人显示了警报对话框。我的应用程序不显示对话框。这是 Main.java:

package com.natasam.mapstwo;

import java.util.List;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.MotionEvent;

import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;



public class Main extends MapActivity {
    MapView map;
    long start;
    long stop;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        map= (MapView)findViewById(R.id.mvMain);
        map.setBuiltInZoomControls(true);   

        Touch t= new Touch();
        List<Overlay>  overlayList = map.getOverlays();
        overlayList.add(t);

    }

    @Override
    protected boolean isRouteDisplayed() {

        return false;
    }

    class Touch extends Overlay{
        public boolean OnTouchEvent(MotionEvent e, MapView v){
            if(e.getAction()== MotionEvent.ACTION_DOWN){
              start=e.getEventTime();

          }
          if(e.getAction()== MotionEvent.ACTION_UP){
              stop=e.getEventTime();
          }
          if(stop- start > 1500){
            Builder alert=new AlertDialog.Builder(Main.this);

              alert.setTitle("pick an option");
              alert.setMessage("You must pick an option");
              alert.setPositiveButton("option 1", new DialogInterface.OnClickListener() {


                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub

                }
            });
              alert.setNegativeButton("pick address", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                    }
                });
              alert.setNeutralButton("option 3", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub

                    }
                });



              alert.create();
              alert.show();
              return true;
          }
            return false;

        }
    }
}

这是 .xml 文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

   <com.google.android.maps.MapView
          android:id="@+id/mvMain"
                 android:layout_width="fill_parent"
                 android:layout_height="fill_parent"
                 android:enabled="true"
                 android:clickable="true"
                 android:apiKey="a valid key" />


</RelativeLayout>

和清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.natasam.mapstwo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
         android:theme="@android:style/Theme.NoTitleBar">
        <uses-library android:name="com.google.android.maps" />
        <activity
            android:name=".Main"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

怎么了?它显示地图但不显示警报对话框...

4

1 回答 1

1

你是在执行OnTouchEvent,不是onTouchEvent

试试这个:

@Override // Yes, use @Override
public boolean onTouchEvent(MotionEvent e, MapView v){

一旦你改变了它,使用android.util.Log.i(...);orSystem.out.println(...);来确保你的触摸事件甚至被调用。

于 2012-08-01T00:46:03.143 回答