2012-07-27 37 views
5

當我按下按鈕時,圖像始終是手機,但我想要的是能夠根據按下哪個按鈕來更改此圖片。選擇基於開關和案例的通知圖像

例如。按鈕imageButton1按下通知圖像設定爲車

NotificationManager nm; 
static final int uniqueID = 101; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Button stat = (Button) findViewById(R.id.btnSet); 
    stat.setOnClickListener(this); 
    ImageButton img1 = (ImageButton) findViewById(R.id.imageButton1); 
    img1.setOnClickListener(this); 
    ImageButton img2 = (ImageButton) findViewById(R.id.imageButton2); 
    img2.setOnClickListener(this); 
    ImageButton img3 = (ImageButton) findViewById(R.id.imageButton3); 
    img3.setOnClickListener(this); 
    ImageButton img4 = (ImageButton) findViewById(R.id.imageButton4); 
    img4.setOnClickListener(this); 
    ImageButton img5 = (ImageButton) findViewById(R.id.imageButton5); 
    img5.setOnClickListener(this); 
    nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
    nm.cancel(uniqueID); 
} 

@SuppressWarnings("deprecation") 
@Override 
public void onClick(View v) { 
    int picture = R.drawable.phone; 
    // TODO Auto-generated method stub 
    switch (v.getId()) { 
    case R.id.imageButton1: 
     // do something 
     picture = (R.drawable.car); 
     break; 
    case R.id.imageButton2: 
     // do something else 
     picture = (R.drawable.clock); 
     break; 
    case R.id.imageButton3: 
     // do something else 
     picture = (R.drawable.globe); 
     break; 
    case R.id.imageButton4: 
     // do something else 
     picture = (R.drawable.little_tv); 
     break; 
    case R.id.imageButton5: 
     // do something else 
     //setImageResource(R.drawable.phone); 
     break; 
    case R.id.btnSet: 
    // do something else 
     EditText etT = (EditText) findViewById(R.id.etTitle); 
     EditText etB = (EditText) findViewById(R.id.etBody); 
     Intent intent = new Intent(this, MainActivity.class); 
     PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0); 
     String setTitle = etT.getText().toString(); 
     String setBody = etB.getText().toString(); 
     String body = setBody; 
     String title = setTitle; 
     Notification n = new Notification(picture, body, System.currentTimeMillis()); 
     n.setLatestEventInfo(this, title, body, pi); 
     n.defaults = Notification.DEFAULT_VIBRATE; 
     nm.notify(uniqueID, n); 
     finish(); 
    break; 
    } 
} 

}

+2

我很抱歉,但我看不到你的問題/問題?你可以添加更多信息嗎? – fkerber 2012-07-27 10:13:55

+0

通過您的代碼,您將在每次通知中看到只有手機圖像 – rajpara 2012-07-27 10:34:57

+0

一切都很好,但是您的問題是什麼?我似乎沒有任何問題? – 2012-07-27 10:35:17

回答

2

刪除聲明從的onClick功能&初始化的'picture'並將其設置爲類變量。

爲了更好地理解參考下

NotificationManager nm; 
static final int uniqueID = 101; 
int picture; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    // Add below line here 
    picture = R.drawable.phone; 

    ...... All your code 

} 

@SuppressWarnings("deprecation") 
@Override 
public void onClick(View v) { 
    // Remove below line 
    // int picture = R.drawable.phone; 

    // TODO Auto-generated method stub 
    switch (v.getId()) { 
    case R.id.imageButton1: 
     // do something 
     picture = (R.drawable.car); 
     break; 
    case R.id.imageButton2: 
     // do something else 
     picture = (R.drawable.clock); 
     break; 
    case R.id.imageButton3: 
     // do something else 
     picture = (R.drawable.globe); 
     break; 
    case R.id.imageButton4: 
     // do something else 
     picture = (R.drawable.little_tv); 
     break; 
    case R.id.imageButton5: 
     // do something else 
     //setImageResource(R.drawable.phone); 
     break; 
    case R.id.btnSet: 
    // do something else 
     EditText etT = (EditText) findViewById(R.id.etTitle); 
     EditText etB = (EditText) findViewById(R.id.etBody); 
     Intent intent = new Intent(this, MainActivity.class); 
     PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0); 
     String setTitle = etT.getText().toString(); 
     String setBody = etB.getText().toString(); 
     String body = setBody; 
     String title = setTitle; 
     Notification n = new Notification(picture, body, System.currentTimeMillis()); 
     n.setLatestEventInfo(this, title, body, pi); 
     n.defaults = Notification.DEFAULT_VIBRATE; 
     nm.notify(uniqueID, n); 
     finish(); 
    break; 
    } 
} 

在你的代碼,只要btnSet獲得點擊「圖片」變量得到與R.drawable.phone初始化,然後根據切換情況下,直接執行「當R .id.btnset」

我覺得你有我的觀點

+0

謝謝您修復了所有需要的全局變量,並將其設置爲 – owenoliver1 2012-07-27 11:01:10

+0

,您歡迎.... – rajpara 2012-07-27 11:02:36