我正在使用 Fragments 开发一个应用程序,并且我的一个 Fragments 上有一个 ImageButton。它应该询问用户是否要保存他们在 EditText 中输入的内容,然后如果他们按下回车键,则将其保存到 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" />
我正在使用此处的技术来覆盖扩展 Fragment Activity 的 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;
}