自定义布局只有一个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的值?
谢谢!
解决方法 我认为这是因为你试图找到你的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:获取输入数据所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)