Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了具有自定义视图的Android AlertDialog:获取输入数据大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个具有AlertDialog的应用程序,显示单个EditText.我遵循 Android开发者指南来做,但是我找不到如何获取用户输入的数据.

自定义布局只有一个EditText:

<EditText
    android:id="@+id/license_value"
    android:inputType="text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@String/license" />

我正在使用DialogFragment创建对话框.当用户点击ok按钮时,我使用一个界面来获取数据.

public class EditLicenseDialogFragment extends DialogFragment {

    public interface EditLicenseDialogListener {
        public void onDialogPositiveClick(DialogFragment dialog,String value);
    }

    private EditLicenseDialogListener mListener;

    // Override the Fragment.onAttach() method to instantiate the NoticeDialogListener
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        // Verify that the host activity implements the callBACk interface
        try {
            // Instantiate the NoticeDialogListener so we can send events to the host
            mListener = (EditLicenseDialogListener) activity;
        } catch (ClassCastException E) {
            // The activity doesn't implement thE interface,throw exception
             throw new ClassCastException(activity.toString()
                    + " must implement NoticeDialogListener");
        }
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceStatE) {
        // Use the Builder class for convenient dialog construction
        AlertDialog.builder builder = new AlertDialog.builder(getActivity());

        builder.settitle(R.String.new_licensE);

        LayoutInflater inflater = getActivity().getLayoutInflater();

        builder.setView(inflater.inflate(R.layout.edit_license,null))

        .setPositiveButton(R.String.ok,new DialogInterface.onClickListener() {
            @Override
            public void onClick(DialogInterface dialog,int id) {
                        // GET VALUE. HOW TO DO IT?
                EditText valueView = (EditText) getActivity().findViewById(R.id.license_value);
                if(valueView == null) Log.d("AA","NULL");
                else{
                    String value = valueView.getText().toString();
                    mlistener.onDialogPositiveClick(EditLicenseDialogFragment.this,value);
                }
            })
        .setNegativeButton(R.String.cancel,new DialogInterface.onClickListener() {
            public void onClick(DialogInterface dialog,int id) {
                EditLicenseDialogFragment.this.getDialog().cancel();
            }
        }); 

        return builder.create();

    }
}

在我的活动中,我执行以下操作:

public class LicenseListActivity extends FragmentActivity 
    implements EditLicenseDialogFragment.EditLicenseDialogListener{

    ...

    findViewById(R.id.buttonAddLicensE).setOnClickListener(
            new View.onClickListener() {
                public void onClick(View view) {
                    DialogFragment dialog = new EditLicenseDialogFragment(true);
                    dialog.show(getSupportFragmentManager(),"EditLicenseDialogFragment");
                }
            });

    @Override
    public void onDialogPositiveClick(DialogFragment dialog,String value) {
       Log.d("TAG",value);
    }

我尝试在DialogFragment中检索的EditText始终为NULl.如何获取EditText的值?

谢谢!

@L_675_12@

我认为这是因为你试图找到你的edittext的观点是不正确的.

应该是这样的

LayoutInflater inflater = getActivity().getLayoutInflater();

View dialogView = inflater.inflate(R.layout.edit_license,null);
builder.setView(dialogView)
.setPositiveButton(R.String.ok,new DialogInterface.onClickListener() {
        @Override
        public void onClick(DialogInterface dialog,int id) {

            EditText valueView = (EditText) dialogView.findViewById(R.id.license_value); //here
            if(valueView == null) Log.d("AA","NULL");
            else{
                String value = valueView.getText().toString();
                mlistener.onDialogPositiveClick(EditLicenseDialogFragment.this,value);
            }
        })

大佬总结

以上是大佬教程为你收集整理的具有自定义视图的Android AlertDialog:获取输入数据全部内容,希望文章能够帮你解决具有自定义视图的Android AlertDialog:获取输入数据所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。