【问题标题】:OnClickListener listener is not working on frame layoutOnClickListener 侦听器不适用于框架布局
【发布时间】:2016-06-08 09:33:03
【问题描述】:

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

这是包含片段框架布局的布局

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

有什么帮助吗?

【问题讨论】:

  • 你能解释一下你的问题吗
  • 但是点击监听器工作正常 - 那么问题出在哪里?
  • 抱歉打错了..click 监听器根本不工作。它没有捕捉到点击事件。
  • 说明:我有一个活动,它具有用于加载片段的框架布局。当片段被加载时,我希望框架布局(包含加载的片段)是可点击的。在我设置点击监听器的情况下框架布局,但它没有捕获事件。

标签: android


【解决方案1】:

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

试试这个例子:

final Button button = (Button) findViewById(R.id.button_id);
         button.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
                 // Perform action on click
             }
         });

【讨论】:

【解决方案2】:

试试这样:

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

【讨论】:

  • 我试过这个,但没有运气。框架布局没有捕捉到触摸事件
【解决方案3】:

先实现OnClickListenerlike

public class Avtivity_Name extends Fragment implements View.OnClickListener {

然后getview里面OnCreateViewlike

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

【讨论】:

    【解决方案4】:

    如果您的视图嵌套过多,可能会发生这种情况。

    例如

     <androidx.constraintlayout.widget.ConstraintLayout    
            <androidx.constraintlayout.widget.ConstraintLayout    
                <androidx.constraintlayout.widget.ConstraintLayout    
                       <androidx.constraintlayout.widget.ConstraintLayout
                           <FrameLayout   
    

    【讨论】:

      【解决方案5】:

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

      【讨论】:

      • 这有意义吗?我的意思是我希望我的框架布局是可点击的,而您建议将其设为可点击 false?
      • 根据您的需要将其设为真/假
      【解决方案6】:

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

      如果在fragment_home xml的基本布局标签中尝试这行代码不起作用

      希望这会对你有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-03-04
        • 2014-03-02
        • 1970-01-01
        • 2014-03-16
        • 1970-01-01
        • 1970-01-01
        • 2023-02-23
        • 2014-01-13
        相关资源
        最近更新 更多