【问题标题】:Handling touch Events for all layouts in xml处理 xml 中所有布局的触摸事件
【发布时间】:2013-04-20 08:56:39
【问题描述】:

我有一个线性布局,其中包含 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...) 请指教。

【问题讨论】:

  • 问题出在哪里??

标签: android android-layout android-widget


【解决方案1】:

对于每个要添加触摸侦听器的布局,设置 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
}

【讨论】:

  • 有没有办法为整个布局xml而不是每个布局编写ontouch监听器
【解决方案2】:

这是一个为约束中的每个元素添加一个 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;
                    }
                });
        }
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-09
    • 2018-08-15
    • 2012-10-26
    相关资源
    最近更新 更多