2011-11-05 117 views
2

我需要一個讓對話框,只要我的程序的用戶,使屏幕上的longclick(在屏幕的任何地方)出現一個對話框...創建在屏幕上點擊(安卓)

所有到目前爲止,我發現的教程只幫助我做出了點擊按鈕時出現的對話框,但那不是我所需要的。

有人可以幫我嗎?

我的代碼現在如下:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    /* In the onCreate() method, we need to set the content to our XML layout, 
    * extract the TabHost object, and call setup() on the TabHost 
    * (we need to do this because our Activity is not a TabActivity). */ 

    tabHost = (TabHost) findViewById(android.R.id.tabhost); 

    tabHost.setup(); 
    tabHost.setOnTabChangedListener(this); 

    //Next, we want to extract our ListView from the XML, set it to a member variable, and add some initial coordinates to its list adapter 
    listView = (ListView) findViewById(R.id.list); 
    listView.setEmptyView((TextView) findViewById(R.id.empty)); 

    // Random points on the geo list: 
    List<GeoPoint> pointsList = new ArrayList<GeoPoint>(); 
    pointsList.add(new GeoPoint((int)(-30.03269*1E6), (int)(-51.225514*1E6))); 
    pointsList.add(new GeoPoint((int)(41.89001*1E6), (int)(12.496948*1E6))); 
    listView.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, pointsList)); 

    // add an onclicklistener to see point on the map 
    listView.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView parent, View view, int position, long id) { 
      GeoPoint geoPoint = (GeoPoint) listView.getAdapter().getItem(position); 
      if(geoPoint != null) { 
       // have map view moved to this point 
       setMapZoomPoint(geoPoint, 12); 
       // programmatically switch tabs to the map view 
       tabHost.setCurrentTabByTag(MAP_TAB_TAG); 
      } 
     } 
    }); 

    // Then, we want to extract our MapView from the XML and set it to a member variable. 
    mapView = (MapView) findViewById(R.id.mapview); 
    mapView.setBuiltInZoomControls(true); 
    mapView.postInvalidate(); 




    final AlertDialog.Builder alert = new AlertDialog.Builder(this); 

    alert.setTitle("make route"); 
    alert.setMessage("from:"); 

    // Set an EditText view to get user input 
    final EditText fromAddress = new EditText(this); 
    alert.setView(fromAddress); 

    alert.setMessage("to:"); 
    final EditText toAddress = new EditText(this); 
    alert.setView(toAddress); 

    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int whichButton) { 
     Editable value1 = fromAddress.getText(); 
     Editable value2 = toAddress.getText(); 
     // Do something with value! 
     } 
    }); 

    Editable value2 = toAddress.getText(); 


    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int whichButton) { 
     // Canceled. 
     } 
    }); 


    View layout = findViewById(R.id.layout); 

    layout.setOnLongClickListener(new View.OnLongClickListener() { 

     public boolean onLongClick(View v) { 
    alert.show(); 
      return false; 
     } 
    }); 

佈局:

<?xml version="1.0" encoding="utf-8"?> 
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <LinearLayout 
     android:id="@+id/layout" 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
     <TabWidget 
      android:id="@android:id/tabs" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" /> 
     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"> 
      <LinearLayout 
       android:orientation="vertical" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:id="@+id/main"> 
       <ListView 
        android:id="@+id/list" 
        android:layout_width="fill_parent" 
        android:layout_height="0dip" 
        android:layout_weight="1" /> 
       <TextView 
        android:id="@+id/empty" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:gravity="center"/> 

      </LinearLayout> 

      <RelativeLayout 
       xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/mainlayout" 
       android:orientation="vertical" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent"> 
       <com.google.android.maps.MapView 
        android:id="@+id/mapview" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:clickable="true" 
        android:apiKey="MY KEY HERE" /> 
      </RelativeLayout> 

     </FrameLayout> 
    </LinearLayout> 
</TabHost> 

該活動是MapActivity,所以我我真的認爲我無法實現長時間點擊在MapActivity上?

回答

2

例如,你可以在佈局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:id="@+id/layout"> 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/hello" /> 

</LinearLayout> 

和活動設置ID爲LineraLayout添加onLongClickListener

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

     View layout = findViewById(R.id.layout); 

     layout.setOnLongClickListener(new View.OnLongClickListener() { 

      @Override 
      public boolean onLongClick(View v) { 
       //show dialog box 
       return false; 
      } 
     }); 
    } 

//編輯

我想你應該AlertDialog。 Builder創建AlertDialog,然後顯示對話框。

警報是你AlertDialog.Builder

AlertDialog alert2 = alert.create(); 
alert2.show() // you have to show AlertDialog, not AlertDialog.Builder 
+0

這沒有幫助,因爲我長時間點擊,沒有任何反應......也許這是因爲我試圖在MapActivity中實現它? – Imperian

+0

如果你可以在這裏發佈代碼,我們會更容易 – Mike

+0

好吧,我編輯了這篇文章。 – Imperian

1
Your Actvity should implements onLongClickListener 

public class Layout1Activity extends Activity implements OnLongClickListener{ 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
    } 

    public boolean onLongClick(View arg0) { 
     // TODO Auto-generated method stub 

/////////////////Perform Action //////////////////// 

     return false; 
    } 
0

我認爲最好的辦法是實現你的活動的onTouchEvent。只是重要的ontouch事件,並把你的代碼裏面。 :)

public boolean onTouchEvent(MotionEvent event) { 
    // TODO Auto-generated method stub 
    return super.onTouchEvent(event); 
}