OnClickListener侦听器不在框架布局上工作

时间:2016-06-08 09:33:04

标签: android

我有一个包含用于片段转换的帧布局的活动。框架布局正在加载片段,现在我想在框架布局上设置单击侦听器。但是点击监听器工作正常。

这是包含片段的帧布局的布局

<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();
        }
    });

任何帮助?

5 个答案:

答案 0 :(得分:0)

我认为你的按钮声明做错了。

试试这个例子:

<ul class="locationlist list-unstyled">
<li>
<div class="checkbox checkbox-success">
<input id="checkbox1" type="checkbox">
<label for="checkbox1">
Vijayanagar
</label>
</div>
</li>
<li>
<div class="checkbox checkbox-success">
<input id="checkbox2" class="test" type="checkbox">
<label for="checkbox2">
Indiranagar
</label>
</div>
</li>
<li>
<div class="checkbox checkbox-success">
<input id="checkbox3" type="checkbox">
<label for="checkbox3">
Rt Nagar
</label>
</div>
</li>
<li>
<div class="checkbox checkbox-success">
<input id="checkbox4" type="checkbox">
<label for="checkbox4">
Rajajinagar
</label>
</div>
</li>
<li>
<div class="checkbox checkbox-success">
<input id="checkbox5" type="checkbox">
<label for="checkbox5">
HSR Layout
</label>
</div>
</li>
<li>
<div class="checkbox checkbox-success">
<input id="checkbox7" type="checkbox">
<label for="checkbox7">
Basavanagudi
</label>
</div>
</li>
<li>
<div class="checkbox checkbox-success">
<input id="checkbox8" type="checkbox">
<label for="checkbox8">
Marathahalli
</label>
</div>
</li>
<li>
<div class="checkbox checkbox-success">
<input id="checkbox10" type="checkbox">
<label for="checkbox10">
Malleswaram
</label>
</div>
</li>
<li>
<div class="checkbox checkbox-success">
<input id="checkbox11" type="checkbox">
<label for="checkbox11">
Banashankari
</label>
</div>
</li>
</ul>
<script>
$(document).ready(function(){
     $('.serchfild').keyup(function(){
    var value = $(this).val();
    $("ul.locationlist > li .checkbox label").each(function() {
        if ($(this).text().search(new RegExp(value, "i")) > -1) {

            $(this).parents('.checkbox').show();
            if($(this).text().search(new RegExp(value, "i"))){
                $(this).css('color','red');
            }

        }
        else {
            $(this).parents('li').hide();
        }

    }); });
});

答案 1 :(得分:0)

试试这样:

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;
    }
}

答案 2 :(得分:0)

首先实现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;
}

并从onCreateView

创建onClick
public void onClick(View v) {
 Toast.makeText(FragmentActivity.this, "I am click listner", Toast.LENGTH_SHORT).show();
}

答案 3 :(得分:0)

像这样添加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" />

答案 4 :(得分:0)

在FrameLayout标记中添加android:clickable="true"

如果它不起作用,请在fragment_home xml

的基本布局标记中尝试此行代码

希望这会对你有所帮助。