gpt4 book ai didi

android - 根据开关和案例选择图像以进行通知

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:12:35 24 4
gpt4 key购买 nike

当我按下按钮时,图像始终是手机,但我想要的是能够根据按下的按钮更改此图片。

例如。按下按钮 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;
}
}

最佳答案

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 get 点击“picture”变量时,使用 R.drawable.phone 进行初始化,然后根据 switch case 直接执行“case R. id.btnset"

我想你明白我的意思了

关于android - 根据开关和案例选择图像以进行通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11685741/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com