Java 单击EditText会导致Button onClick()函数不起作用
我有一个Java 单击EditText会导致Button onClick()函数不起作用,java,android,Java,Android,我有一个expandable列表视图,它的孩子是由四个LinearLayout组成的RelativeLayout,其中包含TextViews,这些视图将获取问题的答案,并为该问题分配1-4分(分别为差、中等、好和世界级),最后是一个EditText,用户可以就他们为什么对得分的问题进行评分留下评论。当用户单击分数框时,背景颜色会改变以突出显示所选分数,并将其他分数框颜色设置回其默认颜色以显示它们不再被选中。下面是我的Java代码,展示了它的工作原理: // ChildView views
expandable列表视图
,它的孩子是由四个LinearLayout
组成的RelativeLayout
,其中包含TextViews
,这些视图将获取问题的答案,并为该问题分配1-4分(分别为差、中等、好和世界级),最后是一个EditText
,用户可以就他们为什么对得分的问题进行评分留下评论。当用户单击分数框时,背景颜色会改变以突出显示所选分数,并将其他分数框颜色设置回其默认颜色以显示它们不再被选中。下面是我的Java代码,展示了它的工作原理:
// ChildView views
badScoreView = convertView.findViewById(R.id.qwedit_main_badView);
mediocreScoreView = convertView.findViewById(R.id.qwedit_main_mediocreView);
goodScoreView = convertView.findViewById(R.id.qwedit_main_goodView);
worldclassScoreView = convertView.findViewById(R.id.qwedit_main_wcView);
badScoreTextView = (TextView) convertView.findViewById(R.id.qwedit_main_badTextView);
mediocreScoreTextView = (TextView) convertView.findViewById(R.id.qwedit_main_mediumTextView);
goodScoreTextView = (TextView) convertView.findViewById(R.id.qwedit_main_goodTextView);
worldClassScoreTextView = (TextView) convertView.findViewById(R.id.qwedit_main_worldclassTextView);
questionCommentEditText = (EditText) convertView.findViewById(R.id.qwedit_main_commentEditText);
// TODO: TextView score subcategory setters
badScoreView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO: add logic to change score
subCategoryScore = 1;
badScoreView.setBackgroundColor(Color.parseColor("#eca9a7"));
mediocreScoreView.setBackgroundColor(Color.parseColor("#f0ad4e"));
goodScoreView.setBackgroundColor(Color.parseColor("#5cb85c"));
worldclassScoreView.setBackgroundColor(Color.parseColor("#0275d8"));
questionCommentEditText.clearFocus();
}
});
mediocreScoreView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO: add logic to change score
subCategoryScore = 2;
badScoreView.setBackgroundColor(Color.parseColor("#d9534f"));
mediocreScoreView.setBackgroundColor(Color.parseColor("#f7d6a6"));
goodScoreView.setBackgroundColor(Color.parseColor("#5cb85c"));
worldclassScoreView.setBackgroundColor(Color.parseColor("#0275d8"));
questionCommentEditText.clearFocus();
}
});
goodScoreView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO: add logic to change score
subCategoryScore = 3;
badScoreView.setBackgroundColor(Color.parseColor("#d9534f"));
mediocreScoreView.setBackgroundColor(Color.parseColor("#f0ad4e"));
goodScoreView.setBackgroundColor(Color.parseColor("#addbad"));
worldclassScoreView.setBackgroundColor(Color.parseColor("#0275d8"));
questionCommentEditText.clearFocus();
}
});
worldclassScoreView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO: add logic to change score
subCategoryScore = 4;
badScoreView.setBackgroundColor(Color.parseColor("#d9534f"));
mediocreScoreView.setBackgroundColor(Color.parseColor("#f0ad4e"));
goodScoreView.setBackgroundColor(Color.parseColor("#5cb85c"));
worldclassScoreView.setBackgroundColor(Color.parseColor("#80baeb"));
questionCommentEditText.clearFocus();
}
});
以及布局,以防相关:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/qwedit_elv_item_score_container"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/qwedit_main_badView"
android:layout_width="232.5dp"
android:layout_height="145.3dp"
android:background="@color/badScore">
<TextView
android:id="@+id/qwedit_main_badTextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="@color/white"
android:textSize="20sp"
android:text="Bad"/>
</LinearLayout>
<LinearLayout
android:id="@+id/qwedit_main_mediocreView"
android:layout_width="232.5dp"
android:layout_height="145.3dp"
android:background="@color/mediocreScore">
<TextView
android:id="@+id/qwedit_main_mediumTextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="@color/white"
android:textSize="20sp"
android:text="Mediocre"/>
</LinearLayout>
<LinearLayout
android:id="@+id/qwedit_main_goodView"
android:layout_width="232.5dp"
android:layout_height="145.3dp"
android:background="@color/goodScore">
<TextView
android:id="@+id/qwedit_main_goodTextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="@color/white"
android:textSize="20sp"
android:text="Good"/>
</LinearLayout>
<LinearLayout
android:id="@+id/qwedit_main_wcView"
android:layout_width="232.5dp"
android:layout_height="145.3dp"
android:background="@color/worldclassScore">
<TextView
android:id="@+id/qwedit_main_worldclassTextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="@color/white"
android:textSize="20sp"
android:text="World Class"/>
</LinearLayout>
</LinearLayout>
<EditText
android:id="@+id/qwedit_main_commentEditText"
android:layout_width="930dp"
android:layout_height="wrap_content"
android:layout_below="@+id/qwedit_elv_item_score_container"
android:cursorVisible="false"
android:focusable="true"
android:focusableInTouchMode="true"
android:inputType="text"
android:hint="Comments"/>
</RelativeLayout>
出于某种原因,当用户按下EditText
并添加文本或从中单击时,按钮onClick()
功能不再工作(即,它们不再改变颜色)
这是因为专注吗?javacode onClick函数位于一个
CustomListViewAdapter
中,以便能够对相同的ExpandableListView
进行充气,以解决我将遇到的问题(大约200个左右)。据我所知,问题在于视图的焦点。您可能需要使用OnFocusChangeListener,然后检查焦点事件是否在屏幕处于触摸模式时发生
或者您可以尝试在每个onClick(视图)方法中添加以下行:
这样,视图将获得焦点,在这些代码行之后,在视图再次设置为setFocusableInTouchMode(false)之后,您可以在onClick()方法中执行某些操作 删除这行:android:focusableInTouchMode=“true”嗯,似乎不是这样。不管这行代码是否存在,问题仍然存在。您建议如何实现OnFocusChangeListener?我尝试过向每个
onClick()
方法添加三行的简单建议,但这并没有解决问题。您是否尝试将这些行添加到包含以下按钮的线性布局中:android:focusable=“true”,android:focusableInTouchMode=“true”?
view.setFocusableInTouchMode(true);
view.requestFocus();
view.setFocusableInTouchMode(false);