0

我有一个线性布局,其中包含 5 个线性布局作为其子级。我想为每个子线性布局处理触摸事件。我的布局看起来像这样

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:id="@+id/container">

             <LinearLayout android:id="@+id/item1"  
            android:layout_width="match_parent"
            style="@style/NavLinkItemContainer"
            android:layout_height="wrap_content">
             <TextView              
                style="@style/NaviLinkSelected"
                android:text="First"/>
            </LinearLayout>

        <LinearLayout android:id="@+id/item2"
            android:layout_width="match_parent"
            style="@style/NavLinkItemContainer"
            android:layout_height="wrap_content">          
        <TextView                  
            style="@style/NaviLinks"
            android:text="Second"/>
        </LinearLayout>

        <LinearLayout android:id="@+id/item3"
            android:layout_width="match_parent"
            style="@style/NavLinkItemContainer"
            android:layout_height="wrap_content">
        <TextView 
            style="@style/NaviLinks"
            android:text="Third"/>
        </LinearLayout>

        <LinearLayout android:id="@+id/item4"
            android:layout_width="match_parent"
            style="@style/NavLinkItemContainer"
            android:layout_height="wrap_content">
        <TextView 
            style="@style/NaviLinks"
            android:text="Fourth"/>
        </LinearLayout>

        <LinearLayout android:id="@+id/item5"
            android:layout_width="match_parent"
            style="@style/NavLinkItemContainer"
            android:layout_height="wrap_content">
        <TextView
            style="@style/NaviLinks"
            android:text="Fifth"/>
        </LinearLayout>

 </LinearLayout>

我使用布局的活动看起来像

public class MainActivity extends Activity implements OnTouchListener{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);  
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        Log.d("","ON TOUCH VIEW ######## "+v.getId());
        return false;
    }
}

触摸子布局时,在 onTouch 事件中没有得到 id(item1,item2...) 请建议。

4

2 回答 2

2

对于要添加触摸侦听器的每个布局,设置 onTouchListener。

例如,

LinearLayout l1 = (LinearLayout) findViewById(R.id.item2);
l1.setOntouchListener(this);

因此,您必须为每个 ViewGroup 设置侦听器。其余的事情已经由您完成。int onTouch 方法可以处理触摸或所有ViewGroup

@Override
public boolean onTouch(View v, MotionEvent event) {
    switch (v.getId()) {
    case R.id.item2:
        do something 
        break;
    case R.id.item3:
        do something 
        break;
    default:
        break;
    }
       // if you want to consume the behavior then return true else retur false
}
于 2013-04-20T08:59:25.263 回答
0

这是一个向约束中的每个元素添加 OnTouchListener 的解决方案,它会在您每次触摸除排除列表中的对象之外的对象时隐藏键盘。

import android.content.Context;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatCallback;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    ConstraintLayout constraintLayout = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final EditText editText = findViewById(R.id.editText);
        //Get current ViewGroup. Might have to add an id in the layout xml for the constraint.
        constraintLayout = findViewById(R.id.constraint);

        //List of items that should be excluded
        ArrayList<Integer> listOfExclusion = new ArrayList<>();
        listOfExclusion.add(editText.getId());


        addTouchListeners(listOfExclusion, constraintLayout);

    }

    /**
     * @param excludedResIds ArrayList of list of ids that should be excluded by the addTouchListener
     * @param parent ViewGroup containing the elements you want to lose focus off.
     */
    void addTouchListeners(ArrayList<Integer> excludedResIds, ViewGroup parent){
        parent.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                constraintLayout.requestFocus();
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                return false;
            }
        });
        addTouchListener(excludedResIds,parent);
    }

    /**
     * @param excludedResIds ArrayList of list of ids that should be excluded by the addTouchListener
     * @param parent ViewGroup containing the elements you want to lose focus off.
     */
    void addTouchListener(ArrayList<Integer> excludedResIds, ViewGroup parent)
    {
        for(int index = 0; index<parent.getChildCount(); ++index) {
            View nextChild = parent.getChildAt(index);
            try{
                addTouchListener(excludedResIds, (ViewGroup) nextChild);
            }catch (Exception e){

            }
            if(!excludedResIds.contains(nextChild.getId()))
                nextChild.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                        constraintLayout.requestFocus();
                        imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                        return false;
                    }
                });
        }
    }

}
于 2019-05-02T18:40:40.893 回答