2013-01-04 54 views
0

我有一個相對佈局一個Android應用程序,我已經把一個按鈕控件這樣Android的按鈕單擊事件是不是爲我工作

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" >  
<Button 
     android:id="@+id/btnCalc" 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="708dp" 
     android:layout_marginLeft="50dp" 
     android:text="@string/btncalc" /> 
</RelativeLayout> 

和測試的事件中,我已放置在此單擊事件敬酒這樣

btncalcu=(Button)findViewById(R.id.btnCalc); 
     btncalcu.setOnClickListener(new Button.OnClickListener(){ 
      @Override 
      public void onClick(View v) 
      { 
       Toast.makeText(getBaseContext(), "a", Toast.LENGTH_LONG).show(); 
       //calc(); 
      } 

     }); 

問題按鈕是,當我運行該應用程序有沒有錯誤或任何異常,但也沒什麼顯示的Toast有。任何能猜到可能b是出的問題.Plz幫助!

+1

你可以在這裏添加完整的活動類代碼嗎? –

+0

僅使用此代替getBaseContext() – Unknown

回答

7

簡單地嘗試這種方式,而不是看它是否正常工作,

btncalcu.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
      Toast.makeText(getBaseContext(), "a", Toast.LENGTH_LONG).show(); 

     } 
    }); 

並確保您已導入下面,

import android.view.View.OnClickListener; 
+0

沒有發生任何事情仍然沒有錯誤沒有消息.. – user1915635

+0

是否嘗試將baseContext更改爲v.getContext()? –

0

使用YourActivity.this的情況下,不使用getBaseContext()

btncalcu=(Button)findViewById(R.id.btnCalc); 
     btncalcu.setOnClickListener(new Button.OnClickListener(){ 
      @Override 
      public void onClick(View v) 
      { 
       Toast.makeText(YourActivity.this, "a", Toast.LENGTH_LONG).show(); 
       //calc(); 
      } 

     }); 

上下文用於告訴應用程序在何處顯示Toast,因此建議使用當前活動。

0

而不是使用getBaseContext(),請使用您擁有的上下文v.getContext()

分辨率:

Toast.makeText(v.getContext(), "a", Toast.LENGTH_LONG).show(); 
0
btncalcu=(Button)findViewById(R.id.btnCalc); 
     btncalcu.setOnClickListener(new Button.OnClickListener(){ 
      @Override 
      public void onClick(View v) 
      { 
       Toast.makeText(yourActivityclassname.this, "a", Toast.LENGTH_LONG).show(); 
       //calc(); 
      } 

     }); 
0

使用這樣的..

btncalcu..setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
      Toast.makeText(getApplicationContext(), "Clicked Button", Toast.LENGTH_LONG).show(); 

      // or 

      Toast.makeText(YourActivityname.this, "Clicked Button",, Toast.LENGTH_LONG).show(); 
     } 
    }); 

爲我工作。希望這來幫你了..

1

剛剛嘗試了這種方式:

btncalcu=(Button)findViewById(R.id.btnCalc); 
    btncalcu.setOnClickListener(new View.OnClickListener(){ <---Change `View.Onclick` instead of `Button` 
     @Override 
     public void onClick(View v) 
     { 
      Toast.makeText(YourActivity.this, "a", Toast.LENGTH_LONG).show(); 
      //calc(); 
     } 
    }); 

並確保您已導入下面,

import android.view.View.OnClickListener;

2

試試這個。

btncalcu=(Button)findViewById(R.id.btnCalc); 
btncalcu.setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     Toast.makeText(MainActivity.this, "Calculation Button clicked", Toast.LENGTH_LONG).show(); 
    } 
}); 

並導入此行

import android.view.View.OnClickListener; 
0

到的方法來解決這個問題:

1.

btncalcu=(Button)findViewById(R.id.btnCalc); 
     btncalcu.setOnClickListener(new OnClickListener(){ 
      @Override 
      public void onClick(View v) 
      { 
       Toast.makeText(YourActivityname.this, "a", Toast.LENGTH_LONG).show(); 
       //calc(); 
      } 

     }); 

2。

btncalcu=(Button)findViewById(R.id.btnCalc); 
      btncalcu.setOnClickListener(new OnClickListener(){ 
       @Override 
       public void onClick(View v) 
       { 
        Toast.makeText(getBaseContext(), "a", Toast.LENGTH_LONG).show(); 
        //calc(); 
       } 

      }); 

,必須檢查有你今年進口進口:

android.view.View.OnClickListener; 
0

使用View.OnClickListener(),而不是Button.OnClickListener()。它會解決你的問題

另外getBaseContext()無疑會爲你工作,但最好使用ActivityName.thisToasts,以防止任何內存泄漏。

0

如果您使用listView檢查是否已應用任何標高。我做了同樣的事情,並在listView頂部有浮動按鈕,因此按鈕從未被點擊過。除非您檢查xml文件,否則無法完成。我寫的代碼僅供您參考。

刪除高程並執行onClick。非常合乎邏輯,在你這樣做之後就可以工作。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 


    <ListView 
     android:id="@+id/fullListViewToDo" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:divider="#F3F3F3" 
     android:dividerHeight="8.0sp" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" 
     android:drawSelectorOnTop="false" 
     android:elevation="8dp"> 

    </ListView> 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:id="@+id/todo_fragmentContainer"> 

    </RelativeLayout> 

    <android.support.design.widget.FloatingActionButton android:id="@+id/fab" 
     android:layout_width="wrap_content" android:layout_height="wrap_content" 
     android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin" 
     android:src="@android:drawable/ic_input_add" 
     android:layout_alignParentEnd="true" 
     android:layout_alignParentBottom="true" /> 

</RelativeLayout>