我正在使用Fragments开发一个应用程序,我的一个片段上有一个ImageButton。它应该询问用户是否要保存他们在EditText中输入的内容,然后如果按Enter键将其保存到SQLiteDatabase。当这个Fragment是一个Activity时,这一切都很好,除了我不必重写onKeyDown。现在我已将它移动到片段,ImageButton什么都不做。我错过了什么?这是代码:
<EditText
android:id="@+id/stitchnotes"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left"
android:hint="@string/hint"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@android:color/white"
android:inputType="text" />
<ImageButton
android:id="@+id/savebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/video_content_description"
android:src="@drawable/actions_file_save_as_icon" />
我正在使用here中的技术来覆盖扩展片段活动的Activity中的onKeyDown方法:
public class StitchList extends FragmentActivity {
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
super.onKeyDown(keyCode, event);
DetailFrag frag = new DetailFrag();
frag.onKeyDown(keyCode, event);
return false;
}
然后在我的片段中定义onKeyDown:
public class DetailFrag extends Fragment {
public static boolean Edited = false;
public AlertDialog.Builder builder;
public AlertDialog alert;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.detailfragment, container, false);
Typeface font = Typeface.createFromAsset(getActivity().getAssets(),
"danielbk.ttf");
builder = new AlertDialog.Builder(getActivity());
builder.setMessage("Do you want to save your Notes?");
builder.setCancelable(false);
builder.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.setNegativeButton("No",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
getActivity().finish();
}
});
alert = builder.create();
final EditText notes = (EditText)view.findViewById(R.id.stitchnotes);
notes.setTypeface(font);
notes.setTextSize(12);
ImageButton savebutton = (ImageButton)view.findViewById(R.id.savebutton);
savebutton.setOnClickListener(new ImageButton.OnClickListener() {
public void onClick(View v) {
String Notes = notes.getText().toString();
Uri updateUri = ContentUris.withAppendedId(STITCHES_URI, rowID);
ContentValues values = new ContentValues();
values.put("stitchnotes", Notes);
getActivity().getContentResolver().update(updateUri,values,null,null);
}
});
notes.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
}
@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
Edited = true;
}
});
return view;
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0
&& Edited) {
alert.show();
return true;
}
else
return false;
}
答案 0 :(得分:2)
现在在onKeyDown
的{{1}}方法中,每次按下某个键时都会创建一个新的片段,但它永远不会将其添加到FragmentActivity
FragmentManager
您希望从@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
super.onKeyDown(keyCode, event);
// Creates a new DetailFrag
DetailFrag frag = new DetailFrag();
// Call the onKeyDown in the new Fragment
frag.onKeyDown(keyCode, event);
return false;
}
获取现有的DetailFrag
,例如:
FragmentManager
或
DetailFrag f = (DetailFrag) getFragmentManager().findFragmentByTag("MyTag");
获得片段后,您可以调用DetailFrag f = (DetailFrag) getFragmentManger().findFragmentById(R.id.fragment);
方法。
此外,在onKeyDown
中,如果您想在点击DetailFrag
时弹出提醒,则需要使用ImageButton
方法拨打alert.show()
。
onClick
然后更新数据库的方法将采用savebutton.setOnClickListener(new ImageButton.OnClickListener() {
public void onClick(View v) {
alert.show();
....
}
});
中onClick
的{{1}}方法,现在只需调用PositiveButton
即可
Dialog