目標這個代碼:監聽器實現:在單個片段3個EditTexts
我有3個EditTexts片段佈局:
- 多線描述
- 十進制量
- 一個日期。
我希望用戶使用軟件鍵盤輸入此信息,然後片段返回一個newItem(記錄)到父活動,因此它可以存儲在數據庫中。
我是否正在使用Listener?
onEditorActionListener
似乎比onKeyListener
更靈活。的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;
}
}
大,經過全體看完之後您的更改我喜歡它們:總結一下,您重命名了界面(謝謝),您添加的按鈕用於「返回」整個記錄,避免需要多個偵聽器(感謝您收拾我的原始提交) – whitep