做这样的事情是可能的
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/addContactButton"
android:text="@string/addContactButtonLabel"
android:onClick="launchContactAdder"/><!-- here -->
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
Java的:
public void launchContactAdder(View v)
{
Intent i = new Intent(this, ContactAdder.class);
startActivity(i);
}
Run Code Online (Sandbox Code Playgroud)
但有一个要求是,该方法必须是公开的,无效的,最重要的是将View作为参数.
现在我想用Checkbox按钮做同样的事情.Checkbox有android:onclick属性,但是在Android教程(http://developer.android.com/resources/samples/ContactManager/index.html)中我可以看到这段代码
showInvisibleControl.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
Log.d(TAG, "mShowInvisibleControl changed: " + isChecked);
showInvisible = isChecked;
populateContactList();
}
});
Run Code Online (Sandbox Code Playgroud)
所以有一个onCheckedChanged(CompoundButton buttonView,boolean isChecked)方法.有没有办法通过XML来做到这一点?没有android:onCheckedChange属性,只有android:onClick属性,但正如我上面写的那样,该属性的名称必须有相应的方法名称,它将View作为参数,但从上面的代码我明白我必须有一个方法CompoundButton和boolean参数.
有什么办法以"XML方式"做到这一点?
这是我的自定义选择器(StateListDrawable)
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/common_cell_background" />
<item
android:state_pressed="true"
android:drawable="@drawable/common_cell_background_highlight" />
<item
android:state_focused="true"
android:drawable="@drawable/common_cell_background_highlight" />
<item
android:state_selected="true"
android:drawable="@drawable/common_cell_background_highlight" />
</selector>
Run Code Online (Sandbox Code Playgroud)
common_cell_background和common_cell_background_highlight都是XML.代码如下:
common_cell_background.xml
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/common_cell_background_bitmap"
android:tileMode="repeat"
android:dither="true">
</bitmap>
Run Code Online (Sandbox Code Playgroud)
common_cell_background_highlight.xml
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/common_cell_background_bitmap_highlight"
android:tileMode="repeat"
android:dither="true">
</bitmap>
Run Code Online (Sandbox Code Playgroud)
位图也完全相同.亮点只是稍微轻一点,没有其他差异.两个位图都是PNG文件.
现在我开始了
convertView.setBackgroundResource(R.drawable.list_item_background);
Run Code Online (Sandbox Code Playgroud)
这是问题所在.我的common_cell_background没有重复,它已经拉长了.但是,当我触摸列表背景的单元格时,令人惊讶的是变为common_cell_background_highlight并猜测是什么?一切都很好,它应该像它应该重复.我不知道问题出在哪里,为什么我的背景不会重复突出显示.有什么想法吗?