2012-07-27 14 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

+0

あなたのコードを介して毎回通知の電話の画像が表示されます – rajpara

+0

すべては問題ありませんが、あなたの質問は何ですか?私は疑問は思わない? –

答えて

2

削除宣言のonClick関数から'picture'の&初期に報知画像を設定し、クラス変数として設定押下されます。 btnSetは「絵」の変数を取得R.drawable.phoneで初期化した後、スイッチケースに応じて、それが直接実行「の場合Rをクリックしてもらう時はいつでもより良く理解するために

はあなたのコードで

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; 
    } 
} 

の下に参照してください。 .id.btnset "

私はあなたが私の意見を持っていると思います。

+0

この固定していただきありがとうございました。必要なものはすべてグローバル変数で、 – owenoliver1

+0

ように設定されていました。 – rajpara

関連する問題