在Fragment内创建对话框的问题(Kotlin)

huangapple go评论75阅读模式
英文:

Problem in creaating dialog Box inside Fragment (Kotlin)

问题

第一次在这里提问,希望能得到答案 在Fragment内创建对话框的问题(Kotlin)
我对Kotlin还很陌生,之前在C++方面已经有3年多的编程经验,最近才开始涉足移动应用开发。
所以我的问题是:
我试图在一个片段(fragment)内创建一个对话框(dialog box),允许用户插入自由文本。
当用户点击加号按钮时,应该打开一个带有自由文本区域的对话框,允许用户插入文本,然后点击“Add”或“Cancel”按钮。
但当我运行程序时,在对话框中我只能看到“Add”和“Cancel”按钮,看不到文本域本身。

但是当我将视图从“fragment_view”更改为“dialog_view”时,我可以看到文本域。
(我希望我在解释问题时表达清楚了)
这是我的片段(fragment_shopping_list)代码:

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        val view = inflater.inflate(R.layout.fragment_shopping_list, container, false)
        val addItem_button = view.findViewById<com.google.android.material.floatingactionbutton.FloatingActionButton>(R.id.fab_shoppiglist)
        addItem_button.setOnClickListener {
            val dialog = AlertDialog.Builder(getContext())
            val tempView = inflater.inflate(R.layout.dialog_shoppinglist, null)
            dialog.setPositiveButton("Add") { _: DialogInterface, _: Int ->

            }
            dialog.setNegativeButton("Cancel") { _: DialogInterface, _: Int ->

            }
            dialog.setView(tempView)
            dialog.show()

        }

        return view
    }

这是我的对话框(dialog_shoppinglist)代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/et_Itemlist"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</RelativeLayout>
英文:

first time asking question here hope ill get an answer 在Fragment内创建对话框的问题(Kotlin)
Im new to Kotlin, i am programming in C++ for 3+ years now and just now i moved to mobile applications
so my question is:
im trying to create dialog box inside fragment that allows the user to insert free field text.
when he click the plus button it should open dialog box with free field text area that allow him to insert text then to click "Add" or "Cancel"
when i do run the program all i see in the dialog box is the buttons "Add" and "Cancel" but i cant see the text field bar itself.

but when i change the view from fragment_view to dialog_view i do see the text field bar.
(i hope i was clear in my problem explanation)
this is my fragment(fragment_shopping_list) code:

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        val view = inflater.inflate(R.layout.fragment_shopping_list, container, false)
        val addItem_button = view.findViewById&lt;com.google.android.material.floatingactionbutton.FloatingActionButton&gt;(R.id.fab_shoppiglist)
        addItem_button.setOnClickListener {
            val dialog = AlertDialog.Builder(getContext())
            val tempView = inflater.inflate(R.layout.dialog_shoppinglist,null )
            dialog.setPositiveButton(&quot;Add&quot;){ _: DialogInterface, _: Int -&gt;

            }
            dialog.setNegativeButton(&quot;Cancel&quot;){ _: DialogInterface, _: Int-&gt;

            }
            dialog.show()

        }

        return view
    } 

and this is my dialog (dialog_shoppinglist) code:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;RelativeLayout
    xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;&gt;
    
    &lt;EditText
        android:id=&quot;@+id/et_Itemlist&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot; /&gt;
&lt;/RelativeLayout&gt;

答案1

得分: 0

请尝试以下代码:

val tempView = inflater.inflate(R.layout.dialog_shoppinglist, null)
val dialog = AlertDialog.Builder(getContext())
    .setPositiveButton("添加") { _: DialogInterface, _: Int ->

    }.setNegativeButton("取消") { _: DialogInterface, _: Int ->

    }.create()

dialog.show()
dialog.window!!.setContentView(tempView)
// 下面的部分是可选的
dialog.window!!.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE)
dialog.window!!.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)

将以下代码添加到按钮的onClick中:

val text = tempView.findViewById<EditText>(R.id.et_Itemlist).text.toString()

这样你就可以获取在对话框中输入的文本了。

英文:

Try this:

val tempView = inflater.inflate(R.layout.dialog_shoppinglist, null)
val dialog = AlertDialog.Builder(getContext())
    .setPositiveButton(&quot;Add&quot;){ _: DialogInterface, _: Int -&gt;

    }.setNegativeButton(&quot;Cancel&quot;){ _: DialogInterface, _: Int-&gt;

    }.create()

dialog.show()
dialog.window!!.setContentView(tempView)
// the following is optional
dialog.window!!.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE)
dialog.window!!.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)

Add this in onClick of button:

val text = tempView.findViewById&lt;EditText&gt;(R.id.et_Itemlist).text.toString

And you can get the text you have input in the dialog.

答案2

得分: 0

val dialog = context?.let { AlertDialog.Builder(it) }
dialog?.setView(R.layout.dialog_shoppinglist)
dialog?.setPositiveButton("添加"){ _: DialogInterface, _: Int ->
}
dialog?.setNegativeButton("取消"){ _: DialogInterface, _: Int ->
}
dialog?.show()
英文:
val dialog = context?.let { AlertDialog.Builder(it) }
dialog?.setView(R.layout.dialog_shoppinglist)
dialog?.setPositiveButton(&quot;Add&quot;){ _: DialogInterface, _: Int -&gt;
}
dialog?.setNegativeButton(&quot;Cancel&quot;){ _: DialogInterface, _: Int-&gt;
}
dialog?.show()

huangapple
  • 本文由 发表于 2020年8月26日 10:44:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63589728.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定