没有调用OnTouch - Android

时间:2017-06-19 08:15:48

标签: android touch

我正在尝试创建一个包含多个EditText s片段的应用。当用户触摸其中一个EditText时,我想在用户可以点击的EditText上方放置一个按钮,该按钮将对EditText执行某些操作。

现在,我在我的片段上实现View.OnTouchListener,但它表现得很奇怪。 首先,当我点击特定EditText时,触摸事件发生但焦点没有改变,因此我无法向EditText写任何内容。

其次,我尝试定位在被触摸的Button上方的EditText不可见。

我有几个片段,它们都是一个进程中的步骤,所以我有一个名为AbstractStep的抽象类,我在这里实现View.OnTouchListener(我省略了代码的某些部分以获得可读性):< / p>

package com.silverfix.dgdeditor.fragments.build;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.silverfix.dgdeditor.BuildActivity;
import com.silverfix.dgdeditor.Constants;
import com.silverfix.dgdeditor.R;
import com.silverfix.dgdeditor.utils.DocumentPack;
import com.silverfix.dgdeditor.utils.files.json.SyncedPackRepository;
import com.silverfix.dgdeditor.utils.files.json.interfaces.OnTaskComplete;
import com.silverfix.dgdeditor.utils.files.json.tasks.ReadTask;
import com.stepstone.stepper.Step;
import com.stepstone.stepper.VerificationError;

import org.parceler.Parcels;

/**
 * Created by David on 27/02/2017.
 */

public abstract class AbstractStep extends Fragment implements Step, View.OnTouchListener {

    protected View setAsDefault;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        setAsDefault = inflater.inflate(R.layout.fill_default_button, null);

        return super.onCreateView(inflater, container, savedInstanceState);
    }

    /*
    * Applies the focus listener of this class to all the edit texts
    * */
    protected void applyOnTouchListener(View... views) {
        for (View v : views) {
            v.setOnTouchListener(this);
        }
    }



    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() != MotionEvent.ACTION_DOWN)
            return false;

        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT
                , ViewGroup.LayoutParams.WRAP_CONTENT);

        params.addRule(RelativeLayout.START_OF, v.getId());
        params.addRule(RelativeLayout.ABOVE, v.getId());

        setAsDefault.setLayoutParams(params);

        return true;
    }
}

以下是AbstractStep的子类的代码:

package com.silverfix.dgdeditor.fragments.build;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.silverfix.dgdeditor.utils.DocumentPack;
import com.silverfix.javadgd.Values;

import com.silverfix.dgdeditor.R;

import java.util.HashMap;

/**
 * Created by David on 14/11/2016.
 */

public class FirstStep extends AbstractStep {

    private TextInputLayout shipperTextArea, consigneeTextArea;
    private EditText aodepEditText, aodesEditText;
    private RadioGroup pacGroup;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.first_phase_fragment, container, false);

        shipperTextArea = (TextInputLayout) rootView.findViewById(R.id.shipper_layout);
        consigneeTextArea = (TextInputLayout) rootView.findViewById(R.id.consignee_layout);


        ViewGroup aodepLayout = (ViewGroup) rootView.findViewById(R.id.aodep_layout);
        ViewGroup aodesLayout = (ViewGroup) rootView.findViewById(R.id.aodes_layout);

        aodepEditText = (EditText) aodepLayout.findViewById(R.id.aodep_field);
        aodesEditText = (EditText) aodesLayout.findViewById(R.id.aodes_field);

        pacGroup = (RadioGroup) rootView.findViewById(R.id.pacLayout);


        if (savedInstanceState != null) {
            shipperTextArea.getEditText().setText(savedInstanceState.getString(Values.Fields.SHIPPER));
            consigneeTextArea.getEditText().setText(savedInstanceState.getString(Values.Fields.CONSIGNEE));

            aodepEditText.setText(savedInstanceState.getString(Values.Fields.AIRPORT_DEPARTURE));
            aodesEditText.setText(savedInstanceState.getString(Values.Fields.AIRPORT_DESTINATION));

            if (savedInstanceState.getBoolean(Values.Fields.PASS_AND_CARGO))
                ((RadioButton) pacGroup.findViewById(R.id.pac_button)).setChecked(true);
            else
                ((RadioButton) pacGroup.findViewById(R.id.cargo_button)).setChecked(true);
        }

        applyOnTouchListener(shipperTextArea.getEditText(), consigneeTextArea.getEditText(), aodepEditText, aodesEditText);

        return rootView;
    }
}

0 个答案:

没有答案