Java Android-复选框不响应FrameLayout内部的单击
我正在开发一个音乐播放器应用程序,我已经创建了一个框架布局,它可以作为一张卡片使用,显示关于每首歌曲专辑艺术、标题、艺术家等的信息。我正在尝试在这张卡片中添加复选框,我计划对其进行自定义,以创建向上投票和向下投票按钮。但是,卡中的复选框不响应单击。如果我在应用程序的主要活动中画一个复选框,它就可以正常工作 以下是卡视图MusicCardView的onDraw方法:Java Android-复选框不响应FrameLayout内部的单击,java,android,checkbox,Java,Android,Checkbox,我正在开发一个音乐播放器应用程序,我已经创建了一个框架布局,它可以作为一张卡片使用,显示关于每首歌曲专辑艺术、标题、艺术家等的信息。我正在尝试在这张卡片中添加复选框,我计划对其进行自定义,以创建向上投票和向下投票按钮。但是,卡中的复选框不响应单击。如果我在应用程序的主要活动中画一个复选框,它就可以正常工作 以下是卡视图MusicCardView的onDraw方法: protected void onDraw(Canvas canvas) { super.onDraw(canvas
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//Draw the voting controls
try {
CheckBox upvoteButton = new CheckBox(getContext());
upvoteButton.setX(20);
upvoteButton.setTop(-60);
upvoteButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// Is the button now checked?
Log.d("MainActivity", "upvote clicked");
}
});
CheckBox downvoteButton = new CheckBox(getContext());
downvoteButton.setX(20);
downvoteButton.setTop(-150);
downvoteButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// Is the button now checked?
Log.d("MainActivity", "downvote clicked");
}
});
upvoteButton.draw(canvas);
downvoteButton.draw(canvas);
} catch (Exception e) {
Log.e("MusicCardView", "exception", e );
}
}
主活动PartyActivity的xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_activity_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.musique.PartyActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/main_linear_view"
android:layout_marginBottom="56dp">
<com.musique.MusicCardView
android:layout_width="match_parent"
android:id="@+id/now_playing_view"
android:layout_height="80dp"
android:background="#ccc"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
app:exampleColor="#000000"
app:songTitle="Money"
app:artist="Pink Floyd"
app:album="The Dark Side of the Moon"
app:exampleDimension="18sp"
app:exampleDrawable="@drawable/darkside"
app:exampleString="example" />
<Space
android:layout_width="match_parent"
android:layout_height="80dp"
android:id="@+id/controller_space"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/query"
android:layout_gravity="center"
android:layout_marginTop="20dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/searchBtn"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="Search"
android:background="#5eca99"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvData"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:animateLayoutChanges="true"
android:id="@+id/main_scroll_view">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/scroll_child">
</LinearLayout>
</ScrollView>
</LinearLayout>
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/navigation" />
</android.support.constraint.ConstraintLayout>
我已经看了和,但没有发布的解决方案运气好,并将非常感谢您可能会有任何建议 如果将控件直接绘制到画布上,它们基本上只是位图,则需要将它们添加到视图层次结构活动/片段布局中,以便它们接收触摸事件
顺便说一句,在onDraw中实例化东西,尤其是视图,这是一个很大的禁忌。upvoteButton.drawcanvas;这是错误的:你必须使用ViewGroupaddView来代替你的复选框,它们会自己绘制并响应你的点击。我不是在回答你的问题,而是想知道你在哪里可以使用上箭头和下箭头绘制?@AJay哇,这是旧代码。编辑以删除。或将触摸事件发送到Checkbox谢谢!我可以问一下为什么在onDraw中实例化不好吗?为了达到所需的60fps并避免出现僵硬的UI,您需要~16毫秒来测量、布局和绘制整个视图层次结构。系统还使用了一些ms。根据您的视图层次结构,这可能是一段相当短的时间来完成工作,因此,您希望在这三种方法之外做尽可能多的工作。实例化对象既耗时又可能导致垃圾回收器启动,既浪费大量时间,也可能浪费帧。
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
MusicCardView v = (MusicCardView) vi.inflate(R.layout.music_card_template, null);
v.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT, 210));
LinearLayout linearLayout = findViewById(R.id.scroll_child);
v.setSongAttributes(song);
new RetrieveArtTask(v).execute(song);
Log.d("PartyActivity", "SongAttributes Set");
linearLayout.addView(v);