用按钮不可点击的Android自定义视图
我创建了自定义RelativeLayout
视图,我使用merge
标签膨胀。
在这个自定义视图中,我有一个按钮,我希望它做某件事。
我试过很多东西,但按钮只是拒绝点击。用按钮不可点击的Android自定义视图
奇怪的部分是,我可以找到的意见,并改变他们的知名度很好。
是否有可能以这种方式点击按钮,还是应该以不同的方式完成?
这个事情我已经试过:
- 匿名内部类为
onClickListener
- XML属性为
onClick
- 查看
onClick
与onClickListener(this)
- 检查它是否是可点击的代码(返回true)
- 向XML和代码添加了
clickable(true)
。 - 从构造函数,而不是
getContext()
- 感动逻辑从
init()
到onFinishInflate()
- 使用的视图从吹气私人方面找观点 从
- 切换到
LayoutInflater
inflate()
充气自定义视图:
<?xml version="1.0" encoding="utf-8"?>
<merge
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/internet_view_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/internet_offline"/>
<custom.IconView
android:id="@+id/internet_view_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="ICON"
android:visibility="invisible"
android:layout_below="@+id/internet_view_text"/>
<Button
android:id="@+id/internet_view_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="BUTTON"
android:clickable="true"
android:layout_below="@+id/internet_view_text"/>
</merge>
父视图的10
相关部分:
<custom.NoInternetView
android:id="@+id/webview_no_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible"
app:automaticReconnect="false"/>
类文件:
public class NoInternetView extends RelativeLayout implements View.OnClickListener {
private static final String TAG = NoInternetView.class.getSimpleName();
private static ConnectivityChangeListener listener;
private static boolean automaticReconnect;
private Context mContext;
private View view;
private Button button;
public NoInternetView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mContext = context;
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.NoInternetView, defStyleAttr, 0);
automaticReconnect = a.getBoolean(R.styleable.NoInternetView_automaticReconnect, false);
a.recycle();
init();
}
public NoInternetView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public NoInternetView(Context context) {
this(context, null);
}
@Override
protected void onFinishInflate() {
Log.d(TAG, "onFinishInflate");
super.onFinishInflate();
button = (Button) view.findViewById(R.id.internet_view_button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "Clicked on some Button");
if (listener != null && NetworkHelper.hasAccess(getContext())) {
listener.connected();
}
}
});
button.setClickable(true);
boolean clicked = button.callOnClick();
Log.d(TAG, "clicked: "+clicked);
TextView text = (TextView) view.findViewById(R.id.internet_view_icon);
text.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "Clicked on some TextView");
if (listener != null && NetworkHelper.hasAccess(getContext())) {
listener.connected();
}
}
});
//check if the attribute 'automaticReconnect' is set to true
//if so, show an icon instead of a button
if (automaticReconnect){
button.setVisibility(INVISIBLE);
view.findViewById(R.id.internet_view_icon).setVisibility(VISIBLE);
}
}
public void init(){
Log.d(TAG, "init");
view = LayoutInflater.from(mContext).inflate(R.layout.no_internet_view, this, false);
}
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
Log.d(TAG, "something happened?");
return super.dispatchKeyEvent(event);
}
@Override
public void onClick(View v) {
Log.d(TAG, "Clicked on Button(TextView)");
if (listener != null && NetworkHelper.hasAccess(getContext())){
listener.connected();
}
}
所以我得到它的工作。以下是我所做的工作。
与
RelativeLayout
更换merge
标签,因为你是充气布局视图中显示。这里更多的信息:Android xml merge layout error on inflate您已设置
android:visibility="invisible"
,更改到android:visibility="visible"
(这是刚拿到视图中可见,你可以在以后改变它编程)-
以下行添加到init()您的NoInternetView的方法。你用所有的视图来扩大布局,但只有当你添加这个视图时,你才能看到它。
public void init(){ Log.d(TAG, "init"); view = LayoutInflater.from(mContext).inflate(R.layout.no_internet_view, this, false); this.addView(view);}
一旦你与这个工作,该按钮将是可见的,点击该按钮可点击。您可以使用匿名onClickListener
或使视图实现onClickListener
并覆盖onClick()
方法。任何工作。
this.addView(view)将导致循环并最终出现内存不足异常。 –
不完全。你已经创建了一个CustomView,这是一个RelativeLayout。您需要在屏幕上显示某些内容,以便在CustomView中扩展布局。问题是,单靠充气是不够的。您需要将该夸大的视图添加到CustomView。我认为你在其他地方做错了一些事情。在进行所有这些修改后,我运行了您的代码。对我来说工作得很好。 – Henry
它是否有任何区别,这种布局加载到片段布局? –
为什么你在这里使用合并标签?您在此处使用的布局仅适用于CustomView中的通货膨胀,因此此处不需要合并。如果用布局类型替换合并,它工作吗? – Henry
@Henry我使用了合并标签,因为我阅读了这个[link](http://trickyandroid.com/protip-inflating-layout-for-your-custom-view/),它显示了层次结构中额外的布局不使用合并标签。另外,当我将合并更改为RelativeLayout时,我的TextView和Button不可见。 –
给我几个小时。我会调试它并让你知道。 – Henry