如何获得数据输出的通用对话类的 [英] How to get data out of a general-purpose dialog class

查看:103
本文介绍了如何获得数据输出的通用对话类的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经决定,我想创建一个可以被一般使用在不同情况下不同的活动,一些自定义对话框类。具体而言,我创建了一个包含一个单一的EditText框,这样一个标题对话框:

I have decided that I wanted to create some custom dialog classes that could be generically used by different Activities in different situations. To be specific, I created a dialog that contains a single EditText box and a title like this:

public class EditTextDialogFragment extends DialogFragment {

    // Factory method to create a new EditTextDialogFragment 
    public static EditTextDialogFragment newInstance( int title, int defaultText ) {
        EditTextDialogFragment frag = new EditTextDialogFragment( );
        Bundle args = new Bundle( );
        args.putInt( "title", title );
        args.putInt( "defaultText", defaultText );
        frag.setArguments( args );
        return frag;
    }

    // Set title and default text
    @Override
    public Dialog onCreateDialog( Bundle savedInstanceState ) {
        // Set an EditText view to get user input
        EditText inputView = new EditText( getActivity( ) );
        inputView.setHint( getArguments( ).getInt( "defaultText" ) );

        AlertDialog.Builder builder = new AlertDialog.Builder( getActivity( ) )
        .setIcon( android.R.drawable.ic_dialog_alert )
        .setTitle( getArguments( ).getInt( "title" ) )
        .setPositiveButton( R.string.ok, ( DialogInterface.OnClickListener )getActivity( ) )
        .setNegativeButton( R.string.cancel, ( DialogInterface.OnClickListener )getActivity( ) )
        .setView( inputView );

        return builder.create( );
    }
}

还请注意,我这样做是使用DialogFragments如在最近的Andr​​oid SDK建议。

Note also that I'm doing this using DialogFragments as suggested in the most recent Android SDK.

总之,这个想法是,从任何活动我能做出这样的呼吁:

Anyway, the idea is that, from any Activity I could make a call like this:

        EditTextDialogFragment etdf = EditTextDialogFragment.newInstance( R.string.add_new_term, R.string.term_name );
        etdf.show( getSupportFragmentManager( ), "new_term" );

和,只要我在活动实施DialogInterface.OnClickListener,我会当用户在对话框中完成得到一个回调。所以我有这个方法在我的活动:

And as long as I implement DialogInterface.OnClickListener in the Activity, I'll get a callback when the user finishes with the dialog. So I have this method in my Activity:

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

    if ( whichButton == DialogInterface.BUTTON_POSITIVE ) {
        Toast.makeText( getApplicationContext( ), "Dialog finished!", Toast.LENGTH_SHORT ).show( );
    }
}

我已经使用这个完全一样的设计,一个是/否对话,它一直伟大的,我喜欢它。我可以轻松地从任何活动只有两个$ C $行C调用它,显示是/否对话框使用自定义的标题。然后onClick的回调包含按钮是pressed所以我有我从对话框中需要的所有信息。

I have used this exact same design for a Yes/No dialog and it has worked great and I love it. I can easily call it from any activity with just two lines of code and show a Yes/No dialog with a customized title. Then the onClick callback contains which button was pressed so I have all the info I need from the dialog.

不过,对于这个对话框的EditText,我无法弄清楚如何获取数据OnClick方法里面的EditText框。传递回来的唯一的事情是按钮pressed和DialogInterface引用(据我所知,并没有帮我拿的EditText上的数据)。

However, for this EditText dialog, I can't figure out how to get the data out of the EditText box inside the OnClick method. The only thing passed back is the button pressed and a DialogInterface reference (which as far as I can tell, doesn't help me get to the EditText data).

我见过的唯一的解决方案涉及嵌入OnClickListener code建设者code像这里面:

The only solutions I have seen involve embedding the OnClickListener code inside the builder code like this:

    builder.setItems(terms, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText( getApplicationContext( ), inputView.getText().toString(), Toast.LENGTH_SHORT ).show( );
        }
    });

但失败创建可以从需要的编辑框任何活动中使用的通用对话类的全部目的。我失去了一些东西明显?这是不可能的?

but that defeats the whole purpose of creating a generic dialog class that can be used from any Activity that needs an EditBox. Am I missing something obvious? Is this impossible?

推荐答案

这是一个非常普遍的问题,其实有一个非常简单的解决方案。你只需要使用新的对话框,认为抢从控制。

This is a very common question and actually has a really easy solution. You just need to use the new Dialog as the view to grab the control from.

事情是这样的..

EditText editTextFromDialog = (EditText)dialog.findViewById(the_editText_ID);

然后在你的onclick处理程序,只要你是在作用域声明的控制(如果没有,你将不得不修改范围,以满足您的需求,您可以只获取的内容。

Then in you the onclick handler, as long as you are in scope for the declared control (if not you will have to modify the scope to suit your needs you can just grab the content.

String dialogInput = editTextFromDialog.getText().toString();

这篇关于如何获得数据输出的通用对话类的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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