所以我有这段代码
var firstKitList = mutableListOf<String>("test", "potato", "another item")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_kit_list)
val mainKitList = kitListView
val mainListViewAdapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, firstKitList)
mainKitList.adapter = mainListViewAdapter
newKitListBtn.setOnClickListener {
// Handler code here.
val intent = Intent(this, NewKitListActivity::class.java)
startActivity(intent);
}
}
对于这个layout。 当我点击按钮,为我显示一个警告对话框时,如何添加一个名称(好像我正在创建一个要添加到该列表中的项目),然后转到下一个活动? (此部分已经创建,您可以在代码中看到)
非常感谢
答案 0 :(得分:10)
我创建了一个用editText保存警告对话框的函数。单击保存名称时,将存储在multableList中并重定向到新活动。
修改后的代码
var firstKitList = mutableListOf<String>("test", "potato", "another item")
// Mutable List for holding names
val nameList = mutableListOf<String>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_kit_list)
val mainKitList = kitListView
val mainListViewAdapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, firstKitList)
mainKitList.adapter = mainListViewAdapter
newKitListBtn.setOnClickListener {
// Show Alert Dialog
showNewNameDialog()
}
}
警报对话功能
fun showNewNameDialog() {
val dialogBuilder = AlertDialog.Builder(this)
val inflater = this.layoutInflater
val dialogView = inflater.inflate(R.layout.custom_dialog, null)
dialogBuilder.setView(dialogView)
val editText = dialogView.findViewById<View>(R.id.editTextName) as EditText
dialogBuilder.setTitle("Custom dialog")
dialogBuilder.setMessage("Enter Name Below")
dialogBuilder.setPositiveButton("Save", { dialog, whichButton ->
//do something with edt.getText().toString();
// Add Name in list
nameList.add(editText.text.toString())
// Handler code here.
val intent = Intent(this, NewKitListActivity::class.java)
startActivity(intent);
})
dialogBuilder.setNegativeButton("Cancel", { dialog, whichButton ->
//pass
})
val b = dialogBuilder.create()
b.show()
}
自定义对话框布局:custom_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:orientation="vertical">
<EditText
android:id="@+id/editTextName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text" />
</LinearLayout>
答案 1 :(得分:0)
我使用kotlin的功能将代码从Rajesh Dalsaniya's answer更改为更加简洁:
fun showNewNameDialog(activity: Activity) {
AlertDialog.Builder(activity).apply {
val dialogView = activity.layoutInflater.inflate(R.layout.custom_dialog, null)
val editText = dialogView.findViewById<EditText>(R.id.editTextName)
setView(dialogView)
setTitle("Custom dialog")
setMessage("Enter Name Below")
setPositiveButton("Save") { _, _ ->
//do something with edt.getText().toString();
// Add Name in list
activity.nameList.add(editText.text.toString())
// Handler code here.
val intent = Intent(activity, NewKitListActivity::class.java)
activity.startActivity(intent);
}
setNegativeButton("Cancel") { _, _ ->
//pass
}
}.create().show()
}
xml保留:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:orientation="vertical">
<EditText
android:id="@+id/editTextName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text" />
</LinearLayout>