此代码的目标:
我有一个包含3个EditTexts的片段布局:
我希望用户使用软件键盘输入此信息,然后Fragment将newItem(记录)返回给父Activity,以便将其存储在db中。
我使用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;
}
}
答案 0 :(得分:1)
问题是,您定义的接口OnEditorActionListener与TextView.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) {
...
答案 1 :(得分: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
。