我正在创建简单的日历应用。之前我使用Button
通过.onClick
事件更改月份图片。但是,当用户点击任何按钮时,通常会更改其backgroundTint
。
然后我使用AppComptButton
,我可以使用setSupportBackgroundTintList
更改颜色。但是出现了新的问题,知道.onClick
事件不起作用并且崩溃了应用程序。
activity_main.xml中:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="344dp"
android:layout_height="360dp"
android:id="@+id/imageView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/Button_row1">
<android.support.v7.widget.AppCompatButton
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/imageView"
android:layout_alignStart="@+id/imageView"
android:layout_below="@+id/imageView"
android:layout_weight="1"
android:onClick="onClick"
android:text="Jan"
/>
MainActivity.java :
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ImageView imageView;
private Calendar calendar;
private int currentMonth;
private SimpleDateFormat thisMonth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showDate();
setColorofButton();
}
public void showDate(){
calendar = Calendar.getInstance();
imageView = (ImageView) findViewById(R.id.imageView);
currentMonth = calendar.get(Calendar.MONTH);
currentMonth = currentMonth + 1;
String convert = Integer.toString(currentMonth);
final int resID = getResources().getIdentifier("pg_000"+convert, "drawable", getPackageName());
imageView.setImageResource(resID);
}
public void setColorofButton(){
final AppCompatButton btn1 = (AppCompatButton) findViewById(R.id.button2);
ColorStateList csl = new ColorStateList(new int[][]{new int[0]}, new int[]{0xffffcc00});
btn1.setSupportBackgroundTintList(csl);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button1:
updateCalander("pg_0001");
break;
case R.id.button2:
updateCalander("pg_0002");
break;
case R.id.button3:
updateCalander("pg_0003");
break;
case R.id.button4:
updateCalander("pg_0004");
break;
case R.id.button5:
updateCalander("pg_0005");
break;
case R.id.button6:
updateCalander("pg_0006");
break;
case R.id.button7:
updateCalander("pg_0007");
break;
case R.id.button8:
updateCalander("pg_0008");
break;
case R.id.button9:
updateCalander("pg_0009");
break;
case R.id.button10:
updateCalander("pg_00010");
break;
case R.id.button11:
updateCalander("pg_00011");
break;
case R.id.button12:
updateCalander("pg_00012");
break;
}
}
public void updateCalander(String month){
imageView = (ImageView) findViewById(R.id.imageView);
final int resID = getResources().getIdentifier(month, "drawable", getPackageName());
imageView.setImageResource(resID);
}
答案 0 :(得分:0)
在AppCompatButton(button1)里面的xml布局中,你指的是imageView,但是你在xml中声明了什么?。