我有一个带有2个按钮的简单应用程序,它会触发通知。后来在编程的进程中我想将正常的buttoms更改为图像buttoms因为更好的设计,所以我删除了正常的buttoms但是图像buttoms in和将id改回原来的 buttom1和buttom2,但现在它不再工作了。我做错了什么?我是否必须将图像按钮与正常图像不同?我必须安装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 :(得分:0)
将此行添加到ImageButtons
android:onClick="sendNotification"
答案 1 :(得分:-1)
你有没有改变
Button yourButton = (ImageButton) findViewById(R.id.btn);
到
ImageButton yourButton = (ImageButton) findViewById(R.id.btn);
此外,如果您向我们展示一些代码,我们将能够在您的查询中为您提供更多帮助。