Android 安卓:许多相同的按钮,onclick不';肯定不行
我在Android 安卓:许多相同的按钮,onclick不';肯定不行,android,button,onclick,android-linearlayout,onclicklistener,Android,Button,Onclick,Android Linearlayout,Onclicklistener,我在LinearLayout <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:la
LinearLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:weightSum="7" >
<Button
android:id="@+id/btn_mon"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_weight="1"
android:background="@color/white"
android:text="0" />
<Button
android:id="@+id/btn_tus"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="0"
android:background="@color/white"
android:layout_weight="1"/>
<Button
android:id="@+id/btn_wen"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="0"
android:background="@color/white"
android:layout_weight="1"/>
<Button
android:id="@+id/btn_thu"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="0"
android:background="@color/white"
android:layout_weight="1"/>
<Button
android:id="@+id/btn_fri"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="0"
android:background="@color/white"
android:layout_weight="1"/>
<Button
android:id="@+id/btn_sat"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="0"
android:background="@color/white"
android:layout_weight="1"/>
<Button
android:id="@+id/btn_sun"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="0"
android:background="@color/white"
android:layout_weight="1"/>
</LinearLayout>
onCalBtnClick方法:
private void onCalBtnClick(Button btn, int day){
Log.d("debug", String.valueOf(day));
btn.setTextColor(mContext.getResources().getColor(R.color.orange));
//selectedYear, month are global
Intent intent = new Intent();
intent.putExtra("year", selectedYear);
intent.putExtra("month", month);
intent.putExtra("day", day);
setResult(RESULT_OK, intent);
finish();
}
但是,如果我将Log.d
放入onCalBtnClick
方法(从每个clicklistener
调用它),只有中间的三个按钮起作用。左侧的两个按钮(btn_mon、btn_tus)和右侧的两个按钮(btn_sat、btn_sun)在用户单击时不起作用。中间的按钮很好用
这是一个类似的问题,但我的布局文件对应于答案中的模式,尽管如此,按钮还是不起作用
更新
当我删除布局文件中固定的按钮高度和宽度时(从50dp到wrap_内容),所有按钮都开始工作了
然而,现在它看起来并不需要。按钮上的文本之间需要空间
主要问题:为什么?设置线性布局的宽度和高度以填充父级。这肯定会起作用。尝试在适配器类之外和初始化工作日数组之后声明所有setOnClickListener按钮 那会解决你的问题 或者尝试为所有按钮设置如下条件:
if (btn_mon != null && position < weekdays.size()) {
btn_mon.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) { onCalBtnClick(btn_mon, weekdays.get(0).data); }});
}
if(btn_mon!=null&&position
如果在布局中使用权重,说明布局中应该填充一些具有一定比例的对象。它与“包装内容”正好相反。使用权重,外部布局定义内部视图的大小,“包裹内容”表示外部布局大小由内部视图定义。
请决定在这种情况下哪种方法更好-删除权重或固定内部视图大小。发布如何初始化btn\u*单击一次显示方法,单击一次尝试登录内部添加您要求的所有内容按钮对胡佛、按下等标准动作作出反应?我的意思是按钮图形正在改变?下一个镜头-尝试从按钮中删除固定的宽度/高度。您是否注意到没有nullPointException?在处理适配器类时,这是一个安全的检查方法。适配器迭代有时起着至关重要的作用。
if (btn_mon != null && position < weekdays.size()) {
btn_mon.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) { onCalBtnClick(btn_mon, weekdays.get(0).data); }});
}