【问题标题】:Android - DialogFragment in a FragmentAndroid - 片段中的 DialogFragment
【发布时间】:2015-03-08 21:43:25
【问题描述】:

我正在尝试在片段类中实现对话片段。基本上我已经在片段类中的一个按钮上设置了一个动作监听器,该按钮将打开一个对话框,用户将在其中输入两个 EdiText 字段然后按确定,它将将该用户输入传递给 SQLite 数据库。

我该如何实现呢?我需要充气什么?我是使用 Fragment 的新手

ublic class tsk extends Fragment {


DatabaseHelper help;
Button add;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_notes, container, false);
    add = (Button) view.findViewById(R.id.addbtn);

    add.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Toast.makeText(getActivity(), "onClick works", Toast.LENGTH_SHORT).show();

            AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
            //LayoutInflater inflater= getActivity().getLayoutInflater();
            //this is what I did to added the layout to the alert dialog
            final EditText titleInput=(EditText)layout.findViewById(R.id.dialog_title);
            final EditText bodyInput=(EditText)layout.findViewById(R.id.dialog_body);

        }
    });
    return view;
}

【问题讨论】:

    标签: android android-activity android-fragments android-dialogfragment


    【解决方案1】:

    AlertDialog.Builder 有一个带有监听器接口实现的 setPositiveButton(也有否定和中性)方法。

       new AlertDialog.Builder(context).setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // Grab edit text content and process it here     
            }
        });
    

    API 文档中有一些内容允许自定义。 http://developer.android.com/reference/android/app/AlertDialog.Builder.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多