Android 侦听器实现:单个片段中包含3个EditText

Android 侦听器实现:单个片段中包含3个EditText,android,fragment,listener,Android,Fragment,Listener,本准则的目标: 我有一个包含3个编辑文本的片段布局: 多行描述 小数 约会 我希望用户使用软件键盘输入此信息,然后片段将一个新项目(记录)返回给父活动,以便将其存储在数据库中 我是否采用了正确的方法来使用侦听器 onEditorActionListener似乎比onKeyListener更灵活 Eclipse对这句话真的很不满意: descriptionEditText.setOnEditorActionListener(此) 抱怨setOnEditorActionListener不适用于参

本准则的目标:

我有一个包含3个编辑文本的片段布局:

  • 多行描述
  • 小数
  • 约会
我希望用户使用软件键盘输入此信息,然后片段将一个新项目(记录)返回给父活动,以便将其存储在数据库中

  • 我是否采用了正确的方法来使用侦听器

    onEditorActionListener
    似乎比
    onKeyListener
    更灵活

  • Eclipse对这句话真的很不满意:

    descriptionEditText.setOnEditorActionListener(此)

    抱怨
    setOnEditorActionListener
    不适用于参数

  • 我假设这意味着我有一个问题:

    public interface OnEditorActionListener {
        public void onNewItemAdded(String[] newItem);
        // need to change this newItem?
    }  
    
    欢迎任何其他评论,因为这是我第一次尝试,我相信这很糟糕

    下面的完整代码。

    import android.app.Activity;
    import android.app.Fragment;
    import android.os.Bundle;
    import android.view.KeyEvent;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.view.inputmethod.EditorInfo;
    import android.widget.EditText;
    import android.widget.TextView;
    
    public class AddNewItemTextFragment extends Fragment {
    
        // A Fragment event callback interface, pg 126 Meier; used to share info with host Activity
        // Host activity listens for a new item to be created
    
        public interface OnEditorActionListener {
            public void onNewItemAdded(String[] newItem);
            // need to change this newItem?
        }
    
        // Create a variable to store a reference to the parent Activity that will implement the interface.
    
        private OnEditorActionListener onEditorActionListener;  
    
        // This reference can be retrieved as soon as the parent has been bound to the Fragment with the Fragment's onAttach handler.
    
        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
            try {
                onEditorActionListener = (OnEditorActionListener)activity;
            } catch (ClassCastException e) {
              throw new ClassCastException(activity.toString() + "must implement OnEditorActionListener");
            }
        }
    
        // The Listener implementation
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstaceState) {
            View view = inflater.inflate(R.layout.add_new_item_text_fragment , container, false);
    
            final EditText descriptionEditText = (EditText)view.findViewById(R.id.description);
            descriptionEditText.setOnEditorActionListener(this);
            final EditText amountEditText = (EditText)view.findViewById(R.id.amount);
            amountEditText.setOnEditorActionListener(this);
            final EditText dateEditText = (EditText)view.findViewById(R.id.date1);
            dateEditText.setOnEditorActionListener(this);
            // add category, sub category Spinner
    
    
            // need to add newItem as a parameter?      
            descriptionEditText.setOnEditorActionListener (new OnEditorActionListener() {
    
                public boolean onEditorAction (TextView v, int actionId, KeyEvent event) {
                    boolean handled = false;
    
                        if (actionId == EditorInfo.IME_ACTION_SEND) {
                            String newDescription = descriptionEditText.getText().toString();
                            String newAmount = amountEditText.getText().toString();
                            String newDate = dateEditText.getText().toString();
                            // add other fields here; create array newItem containing all fields
                            String [] newItem ={newDescription, newAmount, newDate};
    
                            onEditorActionListener.onNewItemAdded(newItem);
                            handled = true;
                        }
                    return handled;  
    
                }
    
    
            });
            return view;
        }
    
    
    }
    

    让我提出另一种方法

    在片段布局中添加一个按钮。
    将其命名为addNewItem
    (“@+id/addNewItem”)

    此按钮允许用户添加新项目

    声明活动必须实现的接口:

    public interface OnAddNewItemListener {
        public void onNewItemAdded(String[] newItem);
    }  
    
    您的片段类:

    public class AddNewItemTextFragment extends Fragment {
    
        // A Fragment event callback interface, pg 126 Meier; used to share info with host Activity
        // Host activity listens for a new item to be created
    
        public interface OnAddNewItemListener {
            public void onNewItemAdded(String[] newItem);
        }
    
        // Create a variable to store a reference to the parent Activity that will implement the interface.
    
        private OnAddNewItemListener onAddNewItemListener;  
    
        // This reference can be retrieved as soon as the parent has been bound to the Fragment with the Fragment's onAttach handler.
    
        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
            try {
                onAddNewItemListener = (OnAddNewItemListener)activity;
            } catch (ClassCastException e) {
              throw new ClassCastException(activity.toString() + "must implement OnEditorActionListener");
            }
        }
    
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.add_new_item_text_fragment , container, false);
    
            final EditText descriptionEditText = (EditText)view.findViewById(R.id.description);
            final EditText amountEditText = (EditText)view.findViewById(R.id.amount);
            final EditText dateEditText = (EditText)view.findViewById(R.id.date1);
            final Button addNewItemButton = (Button)view.findViewById(R.id.addNewItem);    
    
            // onClickListener for button
            addNewItemButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
    
                // need to add newItem as a parameter?
                String newDescription = descriptionEditText.getText().toString();
                String newAmount = amountEditText.getText().toString();
                String newDate = dateEditText.getText().toString();
    
               // add other fields here; create array newItem containing all fields
                String [] newItem ={newDescription, newAmount, newDate};
    
                // call method on Interface(activity)
                onAddNewItemListener.onNewItemAdded(newItem);
               }
            });
    
            return view;
        }
    
    }  
    
    在活动中实现接口。
    单击addNewItem按钮时,将在“活动”中调用方法
    onNewItemAdded

    问题在于,您使用与TextView.OnEditorActionListener相同的名称定义接口OnEditorActionListener。如果要使用第二个,则应在其前面显式写入TextView前缀

    例如:

    public class AddNewItemTextFragment extends Fragment implements TextView.OnEditorActionListener
    
    …有了这个,下一个代码将起作用:

    descriptionEditText.setOnEditorActionListener(this);
    
    ……或者:

     descriptionEditText.setOnEditorActionListener (new TextView.OnEditorActionListener() {
    
                public boolean onEditorAction (TextView v, int actionId, KeyEvent event) {
    ...
    

    谢谢,这绝对是一个有用的评论。我还没有试过,但是EditView.OnEditorActionListener应该和TextView一样工作,否?是的,EditText扩展了TextView,因此OnEditorActionListener是相同的。太好了,在阅读了您所有的更改后,我喜欢它们:为了总结,您重命名了界面(谢谢),并且添加的按钮可以“返回”整条记录,避免了需要多个侦听器(也感谢整理了我最初提交的内容)