2012-07-11 25 views
0

我有一個imageButton。當我點擊ImageButton時。它使父佈局android:id =「@ + id/mainLayout」頂部的可見佈局(childLayout)成爲可見。現在,ChildLayout有三個按鈕(Back,Call,Web)。我實現了OnClickListeners for Back,呼叫& Web按鈕。但是當我點擊「返回」按鈕時它沒有觸發事件。但是當我雙擊按鈕OnClickListener工作。爲什麼OnClickListener不能在單擊時工作?Android:onClickListener for ImageButton

  @Override 
       public void onSingleTapConfirmed() { 
        childLayout = (LinearLayout) findViewById(R.id.childView); 
        childLayout.setVisibility(View.VISIBLE); 
        backButton = (Button)findViewById(R.id.back); 
        web = (ImageButton) findViewById(R.id.web); 
        call = (ImageButton) findViewById(R.id.call); 
        backButton.setOnClickListener(backButtonListener); 
        call.setOnClickListener(callListener); 
        web.setOnClickListener(webListener); 
       } 

      OnClickListener callListener = new OnClickListener() { 

         @Override 
         public void onClick(View v) { 
          Intent callIntent = new Intent(Intent.ACTION_CALL); 
          callIntent.setData(Uri.parse("tel:415-817-9955,’10")); 
          startActivity(callIntent);     
         } 
        }; 

      OnClickListener webListener = new OnClickListener() { 

         @Override 
         public void onClick(View v) { 
          Intent web = new Intent(Intent.ACTION_VIEW); 
          Uri u = Uri.parse("http://google.com"); 
          web.setData(u); 
          startActivity(web); 
         } 
        }; 
      OnClickListener backButtonListener = new OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        finish(); 
       } 
      }; 

和我的XML文件:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/mainLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 


    <LinearLayout 
     android:id="@+id/childView" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/nav_bar" 
     android:padding="5dp" 
     android:visibility="gone" > 

     <Button 
      android:id="@+id/back" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:background="@drawable/title_btn" 
      android:paddingLeft="10dp" 
      android:paddingRight="10dp" 
      android:text="Business" 
      android:textColor="@color/white" > 
     </Button> 

     <TextView 
      android:id="@+id/title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:text="Image" 
      android:textStyle="bold" /> 

     <ImageButton 
      android:id="@+id/call" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/phone" /> 

     <ImageButton 
      android:id="@+id/web" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/web" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical" > 

     <ImageView 
      android:id="@+id/imageView" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" /> 
    </LinearLayout> 

</LinearLayout> 

,或者我應該有使用其他方法來顯示上的ImageButton的單點擊事件子佈局。提前致謝。

+0

向我們展示了XML?描述不是很清楚 – 2012-07-11 05:31:45

+0

我不確定你的意思是「雙擊」。你按下imageButton(第一次點擊),然後按下三個按鈕之一(「雙擊」)?或者你的意思是你必須以某種方式點擊三個按鈕中的一個按鈕兩次? – 2012-07-11 06:01:13

+0

我點擊ImageButton。它使父佈局android:id =「@ + id/mainLayout」頂部的可見佈局(childLayout)成爲可見。現在,ChildLayout有三個按鈕(Back,Call,Web)。我實現了OnClickListeners的Back,Call&Web按鈕。但是當我點擊「返回」按鈕時,它沒有觸發事件。但是當我雙擊按鈕OnClickListener工作。 – user170891 2012-07-11 06:10:20

回答

1

有一個布爾值,在第一次點擊時爲真,如果用戶再次點擊檢查布爾值是否爲真,那麼做雙擊你想做的事情。

你可以定義一個時間假設30毫秒,如果用戶再次點擊你做你想做的事雙擊其他你可以執行單擊操作。

但是在Android中我們做單擊和長按/按,長按可以代替雙擊的東西。建議長按,而不是雙擊。

這裏是相同answer問過,看到該鏈接>>here