我有一个包含以下 xml 和代码的复合 View 。
<?xml version="1.0" encoding="utf-8"?>
<merge
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageButton
android:id="@+id/attachment"
android:background="@drawable/ic_attach"
android:layout_width="48dp"
android:layout_height="48dp"/>
<EditText
android:id="@+id/message"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:maxLines="3"
android:layout_weight="0.8"
android:hint="@string/chatBoxHint"
android:inputType="textMultiLine|textCapSentences"
android:paddingLeft="8dp"/>
<ImageButton
android:id="@+id/send"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="12dp"
android:src="@drawable/ic_send"/>
</merge>
代码:
public class TextUserInputView extends LinearLayout implements View.OnClickListener{
private EditText mMessage;
public TextUserInputView(Context context){
super(context);
inflateView(context);
}
public TextUserInputView(Context context, AttributeSet attrs) {
super(context, attrs);
inflateView(context);
}
public TextUserInputView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
inflateView(context);
}
private void inflateView(Context context){
// set default attribute values
setOrientation(LinearLayout.HORIZONTAL);
setGravity(Gravity.CENTER_VERTICAL);
LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
int padding = (int) context.getResources().getDimension(R.dimen.padding_default);
setPadding(padding, padding, padding, padding);
setLayoutParams(params);
// inflate layout.
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.user_input_text, this);
addListeners();
}
private void addListeners(){
mMessage = (EditText) findViewById(R.id.message);
final ImageButton attachment = (ImageButton) findViewById(R.id.attachment);
final ImageButton send = (ImageButton) findViewById(R.id.send);
mMessage.addTextChangedListener(new TextWatcher()
{
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
}
@Override
public void afterTextChanged(Editable s)
{
if (TextUtils.isEmpty(s))
{
send.setEnabled(false);
} else
{
send.setEnabled(true);
}
}
});
attachment.setOnClickListener(this);
attachment.setVisibility(View.GONE);
send.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
if (v.getId() == R.id.send)
{
Toast.makeText(getContext(), "Text", Toast.LENGTH_SHORT).show();
} else if (v.getId() == R.id.attachment)
{
}
}
}
该 View 正在从 xml 膨胀到一个 Activity 中。
上的点击监听器发送 按钮永远不会被调用。
任何人都可以找出原因吗?也许代码只是需要一些新鲜的眼睛。
最佳答案
我知道您刚刚找到了解决方案,而且问题有点老了,但我想写下这个答案以使其更清晰:
如果从最后一个嵌套 View (即 ImageView)中删除以下两个属性: (android:clickable="true",android:focusable="true"),
它会起作用的。
通过使 ImageView 可点击和可聚焦,它会拦截事件并且不会让父级点击事件:/
而已。
感谢阅读
关于Android onClickListener 在复合 View 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36550778/