2014-01-11 59 views
0

目標這個代碼:監聽器實現:在單個片段3個EditTexts

我有3個EditTexts片段佈局:

  • 多線描述
  • 十進制量
  • 一個日期。

我希望用戶使用軟件鍵盤輸入此信息,然後片段返回一個newItem(記錄)到父活動,因此它可以存儲在數據庫中。

  1. 我是否正在使用Listener?

    onEditorActionListener似乎比onKeyListener更靈活。

  2. 的Eclipse真的是不愉快的線:

    descriptionEditText.setOnEditorActionListener(this);

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


} 

回答

0

讓我推薦另一種方法。

將一個按鈕添加到您的Fragment佈局。
將其命名爲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

+0

大,經過全體看完之後您的更改我喜歡它們:總結一下,您重命名了界面(謝謝),您添加的按鈕用於「返回」整個記錄,避免需要多個偵聽器(感謝您收拾我的原始提交) – whitep

1

問題是,您定義了與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) { 
... 
+0

謝謝這是一個絕對有用的評論。我沒有嘗試過,但EditView.OnEditorActionListener也應該像TextView一樣工作。 ,不是嗎? – whitep

+0

是的,EdiText擴展了TextView,所以OnEditorActionListener是相同的。 – R4ng3LII