调用AlertDialog.Builder在Java中对于Android的碎片当后退按钮pressed [英] Calling AlertDialog.Builder in Java For Android Fragment When Back Button Is Pressed

查看:162
本文介绍了调用AlertDialog.Builder在Java中对于Android的碎片当后退按钮pressed的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的应用程序(真!),其显示的列表,然后显示根据用户的选择列表上的项目的详细信息。我这样做是使用片段。细节部分是它有一个的EditText 片段。目前,如果在的EditText 用户类型和点击保存按钮,一个 AlertDialog.Builder 弹出问她如果她要救。如果她选择是,该文本将被保存到数据库中。我想,如果用户点击后退按钮发生同样的事情。在我的课堂延伸 FragmentActivity ,我有:

I have a simple application (really!) that displays a list and then displays the details of an item on the list based on the user's choice. I do this using Fragments. The details portion is a Fragment which has an EditText in it. Currently, if the user types in the EditText and clicks on the Save button, an AlertDialog.Builder pops up asking her if she wants to save. If she chooses yes, the text is saved to a database. I want the same thing to happen if the user hits the back button. In my class that extends FragmentActivity, I have:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{

    super.onKeyDown(keyCode, event);

    DetailFrag frag = (DetailFrag) getSupportFragmentManager().findFragmentById(R.id.frag_stitchdetail);
    frag.onKeyDown(keyCode, event);
    return false;
}

在我的课堂延伸片段(细节部分),我有:

In my class that extends Fragment (the details portion), I have:

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_ENTER && event.getRepeatCount() == 0
            && Edited) {
        return true;
    }
    else
        return false;
}

我不知道放在哪里code,它会调用 AlertDialog.Builder 。我希望把它放在片段类,因为code需要从获得 ROWID 的细节名单(扩展一个类 ListFragment )。信息不提供给 FragmentActivity 类。对于澄清,这里的code我用操作保存按钮。它从片段类的 onCreateView 方法调用,我希望在后面的按钮是重新使用这种code(把它放在它自己的方法,可能)灾区。

I'm not sure where to put the code that will call the AlertDialog.Builder. I want to put it in the Fragment class because the code needs to get the rowID of the detail from the list (a class that extends ListFragment). That information isn't available to the FragmentActivity class. For clarification, here's the code I use to operate the Save button. It's called from the onCreateView method of the Fragment class and I want to re-use this code (put it in its own method, probably) when the back button is hit.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.detailfragment, container, false);
    Typeface danielFont = Typeface.createFromAsset(getActivity().getAssets(),
            "danielbk.ttf");

    final EditText notes = (EditText)view.findViewById(R.id.stitchnotes);
    notes.setTypeface(danielFont);
    notes.setTextSize(12);

    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) {

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

    builder.setNegativeButton("No",
            new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                    //getActivity().finish();
                }
            });

    alert = builder.create();

    ImageButton savebutton = (ImageButton)view.findViewById(R.id.savebutton);
    savebutton.setOnClickListener(new ImageButton.OnClickListener() {

        public void onClick(View v) {
            alert.show();
        }
    });
    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;
}

ROWID 当我宣布的用于 updateURI 来自ListFragment类,像这样:

The rowID that's used when I declare updateURI comes from the ListFragment class like so:

DetailFrag frag = (DetailFrag) getFragmentManager().findFragmentById(R.id.frag_stitchdetail);
if (frag != null && frag.isInLayout()) {
     //more code
     frag.setRowID(stitchid);
}

setRowID 片段中的类定义:

public void setRowID(int row_id)
{
    rowID = row_id;
}

ROWID 是片段类私有静态诠释。

and rowID is a private static int in the Fragment class.

可有人请点我朝着正确的方向?

Can someone please point me in the right direction?

推荐答案

两个选项,我可以看到。

Two options I can see.

一:

public boolean onKeyDown(int keyCode, KeyEvent event)
{
    if(keyCode == KeyEvent.KEYCODE_BACK) {
        backPressed = true;
        alert.show();
        return true; // shows you consumed the event with your implementation
    }
    // blah, blah, other code
}

以下行添加到您的对话框yes和no OnClickListeners

if (backPressed){
    finish();// - to exit the Activity
}

另外,创建两个单独的建设者如下:

Alternately, create two separate builders as follows:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // other code

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

    builder.setNegativeButton("No",
            new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });

    alertSave = builder.create();


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

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    saveNotes();
                    finish();
                }
            });

    builder.setNegativeButton("No",
            new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                    finish();
                }
            });

    alertBack = builder.create();

    ImageButton savebutton = (ImageButton)view.findViewById(R.id.savebutton);
    savebutton.setOnClickListener(new ImageButton.OnClickListener() {

        public void onClick(View v) {
            alertSave.show();
        }
    });
    // other code
}

public boolean onKeyDown(int keyCode, KeyEvent event)
{
    if(keyCode == KeyEvent.KEYCODE_BACK) {
        alertBack.show();
        return true; // shows you consumed the event with your implementation
    }
    // blah, blah, other code
}        


private void saveNotes() {
    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);
}

这篇关于调用AlertDialog.Builder在Java中对于Android的碎片当后退按钮pressed的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆