2014-09-04 44 views
0

我有一個簡單的應用程序與2 buttoms,它會觸發通知。後來編程的進度我想改變正常的buttoms爲圖像buttoms,因爲更好的設計,所以我刪除了正常的buttoms和但圖像buttoms和改變了id回到原來的 buttom1和buttom2,,但現在它不再工作。我做錯了什麼?我必須編程圖像buttoms不同於正常的?我是否必須安裝libarys或somthing?請幫我!操作不再適用於圖像按鈕?

XML:

<ImageButton 
    android:id="@+id/buttom2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/buttom1" 
    android:layout_below="@+id/buttom1" 
    android:layout_marginTop="23dp" 
    android:src="@drawable/icon2" /> 

<ImageButton 
    android:id="@+id/buttom1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:layout_marginTop="14dp" 
    android:src="@drawable/icon1" /> 

main.java:

public class MainActivity extends ActionBarActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 

    } 


    public void sendNotification(View view) { 

     switch(view.getId()){ 

      case R.id.buttom1: 
       Notification1(); 
       break; 

      case R.id.buttom2: 
       Notification2(); 
       break; 


      } 
     } 

    private void Notification1() { 

     NotificationCompat.Builder builder = new NotificationCompat.Builder(this); 
     builder.setAutoCancel(true); 
     builder.setContentTitle("BasicNotification"); 
     builder.setContentText("Test"); 
     builder.setSmallIcon(R.drawable.icon1); 

     Notification notification = builder.build(); 
     NotificationManager manager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE); 
     manager.notify((int) System.currentTimeMillis(), notification); 

    } 

private void Notification2() { 

     NotificationCompat.Builder builder = new NotificationCompat.Builder(this); 
     builder.setAutoCancel(true); 
     builder.setContentTitle("BasicNotification"); 
     builder.setContentText("Test"); 
     builder.setSmallIcon(R.drawable.icon2); 

     Notification notification = builder.build(); 
     NotificationManager manager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE); 
     manager.notify((int) System.currentTimeMillis(), notification); 

    } 

} 

感謝前面!

+0

添加一些XML ??? – Olayinka 2014-09-04 22:37:59

+0

好吧我編輯它 – DudeEffects 2014-09-04 22:43:49

+0

告訴我你在哪裏定義的按鈕,請 – 2014-09-04 22:47:36

回答

0

此行添加到您的ImageButtons

android:onClick="sendNotification" 
+0

感謝人現在工作正常! – DudeEffects 2014-09-04 23:09:54

+0

嘿只是另一個問題。我怎麼做,當我點擊buttom 2次,第一個通知消失,只有最新的一個留下來?現在好像我按了3次buttom1,icon1是3次在狀態欄中?你知道一個簡單的代碼嗎? – DudeEffects 2014-09-04 23:11:53

+0

@DudeEffects對不起,您應該提出一個新問題 – Olayinka 2014-09-04 23:13:12

-1

是否更改

Button yourButton = (ImageButton) findViewById(R.id.btn); 

ImageButton yourButton = (ImageButton) findViewById(R.id.btn); 

另外,如果你向我們展示一些代碼,我們將能夠更幫助您查詢。

+0

我從未在我的代碼中使用過這樣的行:s 我該在哪裏放置它? – DudeEffects 2014-09-04 22:49:30

+0

你能否粘貼你的onCreate()方法?它應該在那裏 – 2014-09-04 22:50:33

+0

我已經把我現在的所有代碼 – DudeEffects 2014-09-04 22:53:20