下面的代码与我的DatePicker方法有关,您可以选择一个日期,TextView显示所选的日期,如图所示https://i.stack.imgur.com/cco6D.png
我有一个saveData((和loadData((函数,当我从DatePicker中保存数据时,它不会显示用户在我返回创建的Letter后选择的日期。
我目前在我的ActivityMain中有一个setOnClickListener,它在应用程序运行时似乎不会被调用,而且我在saveData((中的Toast I设置在输入和更新数据时也不会显示,并且在Android设备文件资源管理器中的shared_prefs中没有保存任何数据,这让我相信这些可能是阻止我的应用程序保存的问题用户输入了数据,但不确定如何进行修复。
在saveData函数中,我应该在哪里输入setOnClickListener?
activity_layout.xml code
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_timePicker"
android:text="What's the date?"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textTime"
android:layout_marginTop="20dp"
android:textColor="#000000"
android:textSize="22sp"
android:textStyle="bold"
tools:text="@tools:sample/date/ddmmyy"/>
这是ActivityMain中的代码
class NewsActivity : AppCompatActivity(), AnkoLogger, DatePickerDialog.OnDateSetListener, TimePickerDialog.OnTimeSetListener {
var letter = NewsItemModel()
lateinit var app : MainApp
var edit = false
val IMAGE_REQUEST = 1
/* https://www.youtube.com/watch?v=GmmyCOpIutA&ab_channel=CodePalace */
var day = 0
var month = 0
var year = 0
var hour = 0
var minute = 0
var savedDay = 0
var savedMonth = 0
var savedYear = 0
var savedHour = 0
var savedMinute = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_letter)
app = application as MainApp
loadData()
btn_timePicker.setOnClickListener {
saveData()
Log.d("ClickListener", "just got clicked")
}
ratingBar.rating = 2.5f
ratingBar.stepSize = .5f
pickDate()
val dropDownList = arrayOf("Latest", "Coronavirus Updates", "Crime", "Traffic and Travel", "Business", "Politics", "Weather", "Education", "Health")
val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, dropDownList)
adapter.setDropDownViewResource(android.R.layout.simple_spinner_item)
statusFilter.adapter = adapter
statusFilter.onItemSelectedListener = object : AdapterView.OnItemSelectedListener{
override fun onNothingSelected(parent: AdapterView<*>?) {
}
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
if(statusFilter.selectedItemPosition == 1){
condition1()
}
if(statusFilter.selectedItemPosition == 2){
condition2()
}
if(statusFilter.selectedItemPosition == 3){
condition3()
}
if(statusFilter.selectedItemPosition == 4){
condition4()
}
if(statusFilter.selectedItemPosition == 5){
condition5()
}
if(statusFilter.selectedItemPosition == 6){
condition6()
}
if(statusFilter.selectedItemPosition == 7){
condition7()
}
if(statusFilter.selectedItemPosition == 8){
condition8()
}
if(statusFilter.selectedItemPosition == 9){
condition9()
}
}
private fun condition1(){
Toast.makeText(getApplicationContext(), "selected Item: " + statusFilter.selectedItem, Toast.LENGTH_LONG).show()
}
private fun condition2(){
Toast.makeText(getApplicationContext(), "selected Item: " + statusFilter.selectedItem, Toast.LENGTH_LONG).show()
}
private fun condition3(){
Toast.makeText(getApplicationContext(), "selected Item: " + statusFilter.selectedItem, Toast.LENGTH_LONG).show()
}
private fun condition4(){
Toast.makeText(getApplicationContext(), "selected Item: " + statusFilter.selectedItem, Toast.LENGTH_LONG).show()
}
private fun condition5(){
Toast.makeText(getApplicationContext(), "selected Item: " + statusFilter.selectedItem, Toast.LENGTH_LONG).show()
}
private fun condition6(){
Toast.makeText(getApplicationContext(), "selected Item: " + statusFilter.selectedItem, Toast.LENGTH_LONG).show()
}
private fun condition7(){
Toast.makeText(getApplicationContext(), "selected Item: " + statusFilter.selectedItem, Toast.LENGTH_LONG).show()
}
private fun condition8(){
Toast.makeText(getApplicationContext(), "selected Item: " + statusFilter.selectedItem, Toast.LENGTH_LONG).show()
}
private fun condition9(){
Toast.makeText(getApplicationContext(), "selected Item: " + statusFilter.selectedItem, Toast.LENGTH_LONG).show()
}
}
val submission = findViewById<Button>(R.id.submit_button)
submission.setOnClickListener {
Toast.makeText(this, "Button is clicked", Toast.LENGTH_LONG)
}
ratingBar.setOnRatingBarChangeListener { ratingBar, rating, fromUser ->
Toast.makeText(this, "Rating: $rating", Toast.LENGTH_SHORT).show()
}
LetterLocation.setOnClickListener {
startActivity(intentFor<MapsActivity>())
}
chooseImage.setOnClickListener {
showImagePicker(this, IMAGE_REQUEST)
}
if (intent.hasExtra("NewsLetter_edit")) {
edit = true
letterImage.setImageBitmap(readImageFromPath(this, letter.image))
if (letter.image != null) {
chooseImage.setText(R.string.change_letter_image)
}
letter = intent.extras?.getParcelable<NewsItemModel>("NewsLetter_edit")!!
letterTitle.setText(letter.title)
letterDescription.setText(letter.description)
letterAuthor.setText(letter.author)
letterImage.setImageBitmap(readImageFromPath(this, letter.image))
btnAdd.setText(R.string.save_letter)
}
btnAdd.setOnClickListener() {
letter.title = letterTitle.text.toString()
letter.description = letterDescription.text.toString()
letter.author = letterAuthor.text.toString()
if (letter.title.isEmpty()) {
toast(R.string.enter_letter_title)
} else {
if (edit) {
app.letters.update(letter.copy())
} else {
app.letters.create(letter.copy())
}
}
info("add Button Pressed: $letterTitle")
setResult(AppCompatActivity.RESULT_OK)
finish()
}
toolbarAdd.title = title
setSupportActionBar(toolbarAdd)
}
private fun saveData() {
val insertedText = btn_timePicker.text.toString()
val key1 = "STRING_KEY"
val key2 = "BOOLEAN_KEY"
textTime.text = insertedText
val sharedPreferences = getSharedPreferences("sharedPrefs", Context.MODE_PRIVATE) ?: return
with(sharedPreferences.edit()) {
putString(key1, insertedText)
apply()
}
Toast.makeText(this@NewsActivity, "Data Saved", Toast.LENGTH_SHORT).show()
}
private fun loadData() {
val sharedPreferences = getSharedPreferences("sharedPrefs", Context.MODE_PRIVATE)
val savedString = sharedPreferences.getString("STRING_KEY", null)
textTime.text = savedString
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.menu_letter, menu)
if (edit && menu != null) menu.getItem(0).setVisible(true)
return super.onCreateOptionsMenu(menu)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item?.itemId) {
R.id.item_delete -> {
app.letters.delete(letter)
finish()
}
R.id.item_cancel -> {
finish()
}
}
return super.onOptionsItemSelected(item)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {
IMAGE_REQUEST -> {
if (data != null) {
letter.image = data.getData().toString()
letterImage.setImageBitmap(readImage(this, resultCode, data))
chooseImage.setText(R.string.change_letter_image)
}
}
}
}
private fun getDateTimeCalendar(){
val cal = Calendar.getInstance()
day = cal.get(Calendar.DAY_OF_MONTH)
month = cal.get(Calendar.MONTH)
year = cal.get(Calendar.YEAR)
hour = cal.get(Calendar.HOUR)
minute = cal.get(Calendar.MINUTE)
}
private fun pickDate() {
getDateTimeCalendar()
DatePickerDialog(this, this, year, month, day).show()
}
override fun onDateSet(view: DatePicker?, year: Int, month: Int, dayOfMonth: Int) {
savedDay = dayOfMonth
savedMonth = month
savedYear = year
getDateTimeCalendar()
TimePickerDialog(this, this, hour, minute, true).show()
}
override fun onTimeSet(view: TimePicker?, hourOfDay: Int, minute: Int) {
savedHour = hourOfDay
savedMinute = minute
textTime.text = "n $savedDay-$savedMonth-$savedYearn Hour: $savedHour Minute: $savedMinute"
}
fun onClick(v: View?) {
(this@DialogFragment.getActivity() as NewsActivity).onDialogOKPressed()
dismiss()
}
}
在onCreate
中,当活动第一次创建时,您将点击监听器设置为:
override fun onCreate(savedInstanceState: Bundle?) {
...
btn_timePicker.setOnClickListener {
saveData()
}
然后在几行之后,您调用pickDate()
,它可以执行以下操作:
private fun pickDate() {
btn_timePicker.setOnClickListener {
getDateTimeCalendar()
DatePickerDialog(this, this, year, month, day).show()
}
}
您在同一个按钮btn_timePicker
上设置了一个新的点击侦听器,覆盖了调用saveData()
的第一个按钮
你也在这么做。。。
private fun saveData() {
val insertedText = btn_timePicker.text.toString()
...
with(sharedPreferences.edit()) {
putString(key1, insertedText)
btn_timePicker
在这里肯定有用武之地!我甚至不确定你在做什么,它是否可行,但这并不好。在这里,Tenfour04有一个简单的方法来将数据从对话框传递到活动,在活动上创建一个函数,你可以调用并在一段时间内传递,然后你可以保存它或你想做的任何事情:
启动对话框以将值返回到主活动