Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
147 views
in Technique[技术] by (71.8m points)

android - How do you save individual inputted text to each created dynamic button?

On my Homepage I have a plus button. When the user clicks it, it takes them to a second page where they can input a question and an answer (two edit texts). When they're done they click the check button and it takes them back to the homepage where a dynamic button with the text set to the inputted question is created. I used a layout inflator to allow the user to edit the question and answer by clicking the option "Edit" on button click. The problem I'm having is that the saved texts are the last inputted texts. I want it to be set to the originally inputted text for that button specifically. Is there any way to do this?

Homepage:

    package com.example.uh

import android.app.Activity
import android.app.AlertDialog
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.Gravity
import android.widget.Button
import android.widget.LinearLayout
import com.google.android.material.floatingactionbutton.FloatingActionButton

class MainActivity : AppCompatActivity() {
    lateinit var preferences: SharedPreferences
    private val questionActivityCode = 2
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        findViewById<FloatingActionButton>(R.id.btn2).setOnClickListener{
            startActivityForResult(Intent(this@MainActivity, SecondActivity::class.java), questionActivityCode)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if (requestCode == questionActivityCode && resultCode == Activity.RESULT_OK) {
            createNewButtonWithText(data?.getStringExtra("test") ?: "")
        }
    }

    private fun createNewButtonWithText(text: String)
    {
        val newbutton = Button(this@MainActivity)
        val layout = findViewById<LinearLayout>(R.id.mainlayout)
        newbutton.text = text
        newbutton.layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
        newbutton.width=1010
        newbutton.height=300
        newbutton.gravity = Gravity.CENTER
        newbutton.translationX= 65F
        newbutton.setTextColor(Color.parseColor("#FFFFFFFF"))
        newbutton.setBackgroundColor(Color.parseColor("#250A43"))
        layout.addView(newbutton)

        val inflator = layoutInflater
        val builder = AlertDialog.Builder(this)
        val intent = Intent(this@MainActivity, SecondActivity::class.java)

        preferences = getSharedPreferences("SHARED_PREF", Context.MODE_PRIVATE)
        val question = preferences.getString("text", "")
        val answer = preferences.getString("text2", "")
        if (question != null && answer != null) {
            //?
        }
        else{
            //?
        }

        newbutton.setOnClickListener{
            val dialogLayout = inflator.inflate(R.layout.text, null)
            with(builder) {
                setTitle(newbutton.text)
                setPositiveButton("Edit"){dialog, which ->
                    startActivity(intent)
                }
                setNegativeButton("Cancel"){dialog, which ->
                    Log.d("Main", "Negative button clicked")
                }
                setView(dialogLayout)
                show()
            }
        }
    }}

Second Activity:

    package com.example.uh

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.os.Bundle
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.floatingactionbutton.FloatingActionButton

class SecondActivity : AppCompatActivity() {
    lateinit var sharedPreferences: SharedPreferences
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second)


        val question = findViewById<EditText>(R.id.question)
        val answer = findViewById<EditText>(R.id.answer)

        sharedPreferences = getSharedPreferences("SHARED_PREF", Context.MODE_PRIVATE)
        question.setText(sharedPreferences.getString("text", ""))
        answer.setText(sharedPreferences.getString("text2", ""))

        findViewById<FloatingActionButton>(R.id.btn3).setOnClickListener {
            val questiontext = question.text.toString()
            val answertext = answer.text.toString()
            val editor:SharedPreferences.Editor = sharedPreferences.edit()
            editor.putString("text", questiontext)
            editor.putString("text2", answertext)
            editor.apply()


            val returnIntent = Intent()
            returnIntent.putExtra("test", questiontext)
            setResult(Activity.RESULT_OK, returnIntent)

            finish()
        }
    }

}
question from:https://stackoverflow.com/questions/65601821/how-do-you-save-individual-inputted-text-to-each-created-dynamic-button

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...