【发布时间】:2022-02-04 09:47:35
【问题描述】:
我有一个习惯对话片段我用它来捕获用户输入,我将使用它来创建数据库条目。我在用着编辑文本在一个警报对话框.我正在尝试为我的应用程序使用单个活动,而我正在学习的原始教程使用多个活动和意图,但在大多数情况下这似乎已经过时了。
当我调试时,我发现编辑文本正在返回 \"\" 并在我打电话时显示为空TextUtils.isEmpty()在里面主要活动 onDialogPositiveClick.
我已经对这里的表格进行了很多梳理,但我对以下内容感到困惑:
1)我找到的许多答案都是 Java 而不是 Kotlin
2)很多人提到 onCreate 但没有指定 onCreateView 与 onCreateDialog 或者是否只有一个我需要覆盖的 onCreate 。
我对此进行了研究,并找到了一些答案,这些答案让我对何时以及是否需要对布局进行膨胀感到有些困惑。这个当前的迭代我根本没有给它充气。我只是将它设置在警报对话框建设者。
可能是我不理解的界面。我应该如何在对话框和 MainActivity 之间传递信息?该界面似乎通过了对话框本身,但是在从对话框中获取 EditText 时我似乎遗漏了一些东西。
我的自定义 DialogFragment
class NewSongFragment : DialogFragment() {
lateinit var listener: NewSongListener
lateinit var editNewSong: EditText
lateinit var editBPM: EditText
interface NewSongListener {
fun onDialogPositiveClick(dialog: DialogFragment)
fun onDialogNegativeClick(dialog: DialogFragment)
}
/** The system calls this to get the DialogFragment\'s layout, regardless
of whether it\'s being displayed as a dialog or an embedded fragment. */
/*
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
// Inflate the layout to use as dialog or embedded fragment
return inflater.inflate(R.layout.fragment_new_song, container, false)
}
*/
// Override the Fragment.onAttach() method to instantiate the NoticeDialogListener
override fun onAttach(context: Context) {
super.onAttach(context)
// Verify that the host activity implements the callback interface
try {
// Instantiate the NoticeDialogListener so we can send events to the host
listener = context as NewSongListener
} catch (e: ClassCastException) {
// The activity doesn\'t implement the interface, throw exception
throw ClassCastException((context.toString() +
\" must implement NewSongListener\"))
}
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return activity?.let {
// Use the Builder class for convenient dialog construction
val builder = AlertDialog.Builder(it)
//add inflater
//val inflater = requireActivity().layoutInflater;
//val view = inflater.inflate(R.layout.fragment_new_song, null)
builder
.setView(R.layout.fragment_new_song)
.setCancelable(true)
.setNegativeButton(R.string.cancel,DialogInterface.OnClickListener { dialog, id ->
dialog?.cancel()
})
.setPositiveButton(R.string.button_save,
DialogInterface.OnClickListener {dialog, _ ->
listener.onDialogPositiveClick(this)
})
// Create the AlertDialog object and return it
builder.create()
} ?: throw IllegalStateException(\"Activity cannot be null\")
}
}
我的主要活动
class MainActivity : AppCompatActivity(),NewSongFragment.NewSongListener {
private val songViewModel: SongViewModel by viewModels {
SongViewModelFactory((application as SongApplication).repository)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//create view
val recyclerView = findViewById<RecyclerView>(R.id.recycler_view)
val adapter = ItemAdapter(this,
ItemAdapter.OnClickListener { rating -> songViewModel.insertRating(rating) }
)
recyclerView.adapter = adapter
recyclerView.layoutManager = LinearLayoutManager(this)
//initialize data
songViewModel.allSongs.observe(this) { song ->
// Update the cached copy of the songs in the adapter.
song.let { adapter.submitList(it) }
}
// Use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
recyclerView.setHasFixedSize(true)
//add song button
val fab = findViewById<FloatingActionButton>(R.id.fab)
fab.setOnClickListener {
showNewSongDialog()
}
}
private fun showNewSongDialog() {
// Create an instance of the dialog fragment and show it
val dialog = NewSongFragment()
dialog.show(supportFragmentManager, \"NewSongFragment\")
}
override fun onDialogPositiveClick(dialog: DialogFragment) {
// User touched the dialog\'s positive button
val editNewSong = dialog.view?.findViewById<EditText>(R.id.newSongTitle)
val editBPM = dialog.view?.findViewById<EditText>(R.id.newSongBpm)
if(TextUtils.isEmpty(editNewSong?.text)){
}else{
val newSong = Song(editNewSong?.text.toString(),100)
songViewModel.insertSong(newSong)
val rating = Rating(System.currentTimeMillis(),newSong.songTitle, 50)
songViewModel.insertRating(rating)
}
}
override fun onDialogNegativeClick(dialog: DialogFragment) {
// User touched the dialog\'s negative button
}
}
-
这就是我开始的地方:[developer.android.com/codelabs/… 事实证明,从那时起,谷歌就转向了单一的活动和片段,所以我试图使用这个 [developer.android.com/guide/topics/ui/dialogs.html?hl=en]
标签: android kotlin android-edittext android-alertdialog android-dialogfragment