英文:
Problem in creaating dialog Box inside 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
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<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.show()
}
return view
}
and this is my dialog (dialog_shoppinglist) code:
<?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>
答案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("Add"){ _: DialogInterface, _: Int ->
}.setNegativeButton("Cancel"){ _: DialogInterface, _: Int->
}.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<EditText>(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("Add"){ _: DialogInterface, _: Int ->
}
dialog?.setNegativeButton("Cancel"){ _: DialogInterface, _: Int->
}
dialog?.show()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论