我的EditText仅由SetText()更新;每两次尝试

时间:2018-10-29 17:23:42

标签: java android android-edittext settext

我正在尝试在EditText框中显示日期和时间,并在此处做了一些代码。问题是,它正在起作用-但仅在第二次尝试时才起作用。意思是我选择了日期和时间。什么都没有发生,第二次尝试setText();。更新EditText以显示日期和时间。但是,您可能已经猜到了,我希望它立即更新EditText。

using System;
using UnityEditor;
using UnityEngine;
using System.Collections;
using System.Reflection;

public class HierarchyEditor : EditorWindow
{
    private static SearchableEditorWindow hierarchy { get; set; }
    private string filterText = "";

    [MenuItem("Tools/Hierarchy Editor")]
    public static void ShowWindow()
    {
        GetWindow<HierarchyEditor>("HierarchyEditor");
    }
    private void OnGUI()
    {
        CustomHierarchy.gameObjectFontColor = EditorGUILayout.ColorField("Original Font Color", CustomHierarchy.gameObjectFontColor);
        CustomHierarchy.prefabOrgFontColor = EditorGUILayout.ColorField("Prefab Original Font Color", CustomHierarchy.prefabOrgFontColor);
        CustomHierarchy.prefabModFontColor = EditorGUILayout.ColorField("Prefab Modified Font Color", CustomHierarchy.prefabModFontColor);
        CustomHierarchy.inActiveColor = EditorGUILayout.ColorField("Inactive Color", CustomHierarchy.inActiveColor);
        CustomHierarchy.meshRendererColor = EditorGUILayout.ColorField("Mesh Renderer Color", CustomHierarchy.meshRendererColor);

        filterText = GUI.TextField(new Rect(30,190,120,30),filterText, 25);

        SetSearchFilter(filterText, 1);
    }

    public const int FILTERMODE_ALL = 0;
    public const int FILTERMODE_NAME = 1;
    public const int FILTERMODE_TYPE = 2;

    public static void SetSearchFilter(string filter, int filterMode)
    {
        SearchableEditorWindow[] windows = (SearchableEditorWindow[])Resources.FindObjectsOfTypeAll(typeof(SearchableEditorWindow));

        foreach (SearchableEditorWindow window in windows)
        {
            if (window.GetType().ToString() == "UnityEditor.SceneHierarchyWindow")
            {
                hierarchy = window;
                break;
            }
        }

        if (hierarchy == null)
            return;

        MethodInfo setSearchType = typeof(SearchableEditorWindow).GetMethod("SetSearchFilter", BindingFlags.NonPublic | BindingFlags.Instance);
        object[] parameters = new object[] { filter, filterMode, false };

        setSearchType.Invoke(hierarchy, parameters); 
    }
}

我已经尝试了几个小时来解决此问题,在Stackoverflow和许多其他站点上都进行了查找。这有点令人沮丧,我真的希望有人对我做错的事情有所投入。

很抱歉,如果我错过了该网站,已经有了某种解决方案。 非常感谢。

1 个答案:

答案 0 :(得分:0)

我找到了解决方案,并添加了EditText-如注释中所建议。因此,与其尝试同时设置日期和时间字符串,不如尝试设置EditText的文本。我把它们分开,做了一个setText(date);并附加时间:

  mDateSetListener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
            month = month + 1;
            final String date = dayOfMonth + "/" + month + "/" + year;
            mDisplayDate.setText(date);
        }
    };
    mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
        @Override
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            if (minute < 10) {
                String time = " " + hourOfDay + ":" + "0" + minute;
                mDisplayDate.append(time);
            } else {
                String time = " " + hourOfDay + ":" + minute;
                mDisplayDate.append(time);

            }
        }
    };