0

我正在使用 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;
}
4

1 回答 1

2

现在在你的onKeyDown方法中,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;
}

您想从 中获取现有DetailFragFragmentManager,例如:

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();
        ....
    }
});

然后更新数据库的方法将进入 in 的onClick方法中PositiveButtonDialog现在它只是调用dialog.cancel()which just dis

builder.setPositiveButton("Yes",
        new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

                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);
            }
        });
于 2012-11-26T23:24:44.587 回答