简体   繁体   English

根据Spinner值设置EditText可见性

[英]Set EditText visibility based on Spinner value

I'm trying to create this dialog: 我正在尝试创建此对话框:

对话 .

When Spinner is set to custom value, TextEdit should automatically appear. 当Spinner设置为自定义值时,TextEdit应该会自动出现。 I'm calling View.setVisible() on the TextView but the visibility is not evaluated immediately but waits to another change - eg adding another row or setting a date. 我在TextView上调用View.setVisible() ,但是可见性不会立即评估,而是等待其他更改-例如添加另一行或设置日期。

The code: 编码:

        ...
        customText = (EditText) v.findViewById(R.id.edit_custom_text);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        s.setAdapter(adapter);

        s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                SpinnerItem si = (SpinnerItem) adapterView.getItemAtPosition(i);
                evt.type = si.eventType;
                if (evt.type == EventType.CUSTOM) {
                    customText.setVisibility(View.VISIBLE);
                } else {
                    customText.setVisibility(View.GONE);
                }
            }


            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
                //do nothing
            }
        });

I tried View.invalidate() (on parent view) and View.refreshDrawableState() with no luck :/ 我在没有运气的情况下尝试了View.invalidate() (在父视图上)和View.refreshDrawableState()

Edit: The code above is reached (verified by debugger) and I also tried View.INVISIBLE . 编辑:到达上面的代码(由调试器验证),我也尝试了View.INVISIBLE The view is just not refreshed immediately but only after another change in view. 该视图只是不会立即刷新,而是仅在视图发生另一次更改后才刷新。

That should work, could it be that your layout somehow doesn't allow/recognises this change perhaps? 那应该行得通,可能是您的布局以某种方式不允许/识别此更改?

Try changing it to INVISIBLE instead of GONE , including (important!) in your layout xml file. 尝试将其更改为INVISIBLE而不是GONE ,包括在布局xml文件中的(重要!)。

If that works for some reason, try something like this: 如果由于某些原因而起作用,请尝试如下操作:

customText.getParent().requestLayout(); //possibly the parent of that etc

For Example see this 例如看这个

    s.setOnItemSelectedListener(new OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parentView,View selectedItemView, int position, long id) {
    if ("YES".equals(s.getSelectedItem().toString().toUpperCase())) {
    youredittxt.setVisibility(View.VISIBLE);

    } else if ("NO".equals(s.getSelectedItem().toString().toUpperCase())) {
youredittxt.setVisibility(View.INVISIBLE);
}}
    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub
    }
    });

As a follow up question, are you in the main UI thread? 作为后续问题,您是否在主UI线程中? Because Android have some built in features and policies, so only the owning thread will be able to change the UI. 由于Android具有一些内置的功能和策略,因此只有拥有的线程才能更改UI。 If you are outside the same thread, try: 如果您不在同一线程中,请尝试:

customText.getHandler().post(new Runnable() {
    public void run() {
        customText.setVisibility(View.VISIBLE);
    }
});

Hope this helps! 希望这可以帮助! :) :)

验证您实际上正在到达代码块。

  customText.setVisibility(View.GONE); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM