我试图通过调用输入上的setText
方法来更改片段内的editText的值。但它并不总是有效:
这里是片段定义(使用kotlinX扩展来攻击小部件)
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.Spinner
import com.loloof64.android.basicchessendgamestrainer.PositionGeneratorValuesHolder
import com.loloof64.android.basicchessendgamestrainer.R
import kotlinx.android.synthetic.main.fragment_editing_other_pieces_global_constraint.*
import java.lang.ref.WeakReference
import java.util.logging.Logger
class OtherPiecesGlobalConstraintEditorFragment : Fragment() {
private var spinnerPiecesKindValues = listOf<PieceKind>()
private var lastSpinnerSelectedItem : PieceKind? = null
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_editing_other_pieces_global_constraint, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
generator_editor_spinner_other_piece_global_constraint.
onItemSelectedListener =
OtherPiecesGlobalConstraintEditorSpinnerSelectionListener(this)
generator_editor_field_other_piece_global_constraint.
onFocusChangeListener =
OtherPiecesGlobalConstraintEditorScriptFieldFocusListener(this)
updatePieceKindsSpinnerAndLoadFirstScriptIfAny()
}
override fun onResume() {
super.onResume()
updatePieceKindsSpinnerAndLoadFirstScriptIfAny()
}
private fun updatePieceKindsSpinnerAndLoadFirstScriptIfAny() {
loadSpinnerTitles()
loadScriptMatchingSpinnerSelectionOrDisableAndClearField()
if (scriptsByPieceKind.isNotEmpty()) {
generator_editor_spinner_other_piece_global_constraint.setSelection(0)
}
}
companion object {
fun newInstance(): OtherPiecesGlobalConstraintEditorFragment {
return OtherPiecesGlobalConstraintEditorFragment()
}
fun deleteScriptAssociatedWithPieceKind(kind: PieceKind){
scriptsByPieceKind.remove(kind)
}
val scriptsByPieceKind = mutableMapOf<PieceKind, String>()
}
private fun loadSpinnerTitles() {
val pieceTypesStrings = resources.getStringArray(R.array.piece_type_spinner)
val sideStrings = resources.getStringArray(R.array.player_computer_spinner)
spinnerPiecesKindValues =
PositionGeneratorValuesHolder.otherPiecesCount.map { it.pieceKind }
val otherPiecesKinds = spinnerPiecesKindValues.map {
"${pieceTypesStrings[it.pieceType.ordinal]}
${sideStrings[it.side.ordinal]}"
}.toTypedArray()
val spinnerAdapter = ArrayAdapter<String>(activity, android.R.layout.simple_spinner_item, otherPiecesKinds)
spinnerAdapter.
setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item)
generator_editor_spinner_other_piece_global_constraint.adapter = spinnerAdapter
setEmptyScriptsWheneverScriptMissingForASpinnerKey()
}
private fun setEmptyScriptsWheneverScriptMissingForASpinnerKey(){
for (currentSpinnerKey in spinnerPiecesKindValues){
if ( ! scriptsByPieceKind.containsKey(currentSpinnerKey) )
{
scriptsByPieceKind[currentSpinnerKey] = ""
}
}
}
fun loadScriptMatchingSpinnerSelectionOrDisableAndClearField(){
if (spinnerPiecesKindValues.isEmpty()){
generator_editor_field_other_piece_global_constraint.
text.clear()
generator_editor_field_other_piece_global_constraint.
isEnabled = false
}
else {
val selectedItemPosition =
generator_editor_spinner_other_piece_global_constraint.
selectedItemPosition
val selectedPieceKind =
spinnerPiecesKindValues[selectedItemPosition]
val associatedScript =
scriptsByPieceKind[selectedPieceKind]
generator_editor_field_other_piece_global_constraint.
isEnabled = true
//----------------------------------------//
// Here is the strange editText behaviour //
//----------------------------------------//
generator_editor_field_other_piece_global_constraint.
setText(associatedScript)
generator_editor_field_other_piece_global_constraint.postInvalidate()
/////////////////////////////////////////////////
Logger.getLogger("loloof64").info("selected piece kind : $selectedPieceKind")
Logger.getLogger("loloof64").info("associated script : $associatedScript")
Logger.getLogger("loloof64").info("current script field value : ${generator_editor_field_other_piece_global_constraint.text}")
/////////////////////////////////////////////////
}
}
fun retainCurrentScript() {
if (lastSpinnerSelectedItem != null){
scriptsByPieceKind[lastSpinnerSelectedItem!!] =
generator_editor_field_other_piece_global_constraint.
text.toString()
}
}
fun removeScriptFieldFocus() {
generator_editor_field_other_piece_global_constraint.clearFocus()
}
fun updateLastSelectedItem() {
val spinnerSelectedItemPosition =
generator_editor_spinner_other_piece_global_constraint.
selectedItemPosition
lastSpinnerSelectedItem =
if (spinnerSelectedItemPosition == Spinner.INVALID_POSITION) null
else spinnerPiecesKindValues[spinnerSelectedItemPosition]
}
}
class OtherPiecesGlobalConstraintEditorSpinnerSelectionListener(parent:
OtherPiecesGlobalConstraintEditorFragment):
AdapterView.OnItemSelectedListener {
override fun onNothingSelected(parent: AdapterView<*>?) {
saveLastScriptAndSetToCurrent()
}
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
saveLastScriptAndSetToCurrent()
}
private fun saveLastScriptAndSetToCurrent(){
parentRef.get()?.removeScriptFieldFocus()
parentRef.get()?.updateLastSelectedItem()
parentRef.get()?.
loadScriptMatchingSpinnerSelectionOrDisableAndClearField()
}
private val parentRef = WeakReference(parent)
}
class OtherPiecesGlobalConstraintEditorScriptFieldFocusListener(parent:
OtherPiecesGlobalConstraintEditorFragment):
View.OnFocusChangeListener
{
override fun onFocusChange(v: View?, hasFocus: Boolean) {
if (!hasFocus){
parentRef.get()?.retainCurrentScript()
}
}
private val parentRef = WeakReference(parent)
}