OnClickListener监听器在帧布局上不起作用
我有一个包含片段转换帧布局的活动。框架布局正在加载一个片段,现在我想在帧布局上设置点击监听器。但点击侦听器工作正常。OnClickListener监听器在帧布局上不起作用
这里是包含片段
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".FragmentActivity"
tools:showIn="@layout/app_bar_balance"
android:id="@+id/layout">
<FrameLayout
android:name="com.dd.cotech.Fragments.HomeFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragmentWindow"
tools:layout="@layout/fragment_home" />
这里的框架布局布局听者编码
findViewById(R.id.fragmentWindow).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(FragmentActivity.this, "I am click", Toast.LENGTH_SHORT).show();
}
});
任何帮助吗?
我认为你做错了按钮的声明。
试试这个例子:
final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
我不想在按钮上设置侦听器,但在框架布局上。 –
嗯,好吧,试试这个链接http://stackoverflow.com/questions/16977873/framelayout-click-event-is-not-firing是真的比你的问题 – Simpson
试试这样说:
public class FragmentActivity implements LocationListener, OnTouchListener {
private FrameLayout mFrame;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mFrame = (FrameLayout) findViewById(R.id.fragmentWindow);
mFrame.setOnTouchListener(this);
}
public boolean onTouch(View v, MotionEvent e) {
if(mFrame == v) {
Toast.makeText(FragmentActivity.this, "I am click", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
}
我试过这个,但没有运气。框架布局不是触摸事件 –
首先实现OnClickListener
像
public class Avtivity_Name extends Fragment implements View.OnClickListener {
然后里面OnCreateView
getview像
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View rootview = inflater.inflate(R.layout.main, container, false);
FrameLayout layout = (FrameLayout) rootview.findViewById(R.id.fragmentWindow);
layout.setOnClickListener(this);
return rootview;
}
,创造出onClick
侧onCreateView
public void onClick(View v) {
Toast.makeText(FragmentActivity.this, "I am click listner", Toast.LENGTH_SHORT).show();
}
添加clickable
属性是这样的:
<FrameLayout
android:name="com.dd.cotech.Fragments.HomeFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragmentWindow"
tools:layout="@layout/fragment_home"
android:clickable="false" />
它有任何意义吗?我的意思是我希望我的框架布局是可点击的,并且您推荐使其可点击为假? –
只要根据您的需求进行判断即可 –
中的FrameLayout标签添加android:clickable="true"
。
如果它不工作,一旦尝试fragment_home XML
希望这将帮助你的基地布局标签这行代码。
你能解释更多关于你的问题吗 –
但是点击侦听器工作正常 - 那么问题是什么? –
对于输入错误感到抱歉.click监听器根本无法正常工作。它不会捕获单击事件。 –