首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ViewSwitcher不去之前的视图

ViewSwitcher不去之前的视图
EN

Stack Overflow用户
提问于 2016-07-24 14:47:27
回答 2查看 319关注 0票数 1

你好,我正在尝试使用viewswitcher往返于TextViewEditText之间。我的视图从edittext切换到textview。但不会掉头。这是我的xml

代码语言:javascript
复制
<ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/noteItemVS"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true">

            <TextView
                android:id="@+id/noteItemTextView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clickable="true"
                android:text="textview"
                android:textAlignment="center" />

            <EditText
                android:id="@+id/noteItemEditText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="edittext"
                android:textAlignment="center" />

        </ViewSwitcher>

我的切换代码是:

代码语言:javascript
复制
protected void switchViews(NoteItem note) {
        ViewSwitcher switcher = (ViewSwitcher)      context.findViewById(R.id.noteItemVS);
        View switchCheck = switcher.getCurrentView();
        if (switchCheck instanceof EditText) {
            EditText l = (EditText)switchCheck ;
            String x = l.getText().toString();
            Log.e("tree",x);
            if (!x.isEmpty()) {
                switcher.showPrevious();
                TextView myTV = (TextView) switcher.findViewById(R.id.noteItemTextView);
                myTV.setText(x);
            }
        }
        ; if (switchCheck instanceof TextView) {
            TextView l = (TextView) switchCheck;
            String x = l.getText().toString();
            switcher.showNext();
            EditText myEditText = (EditText) switcher.findViewById(R.id.noteItemEditText);
            myEditText.setText(x);
        }
    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-07-24 17:26:22

这是因为解释器正在同时遍历if语句,而默认情况下,后者将覆盖forment语句的值。尝试下面的代码,它肯定会工作的。

代码语言:javascript
复制
protected void switchViews() {
    ViewSwitcher switcher = (ViewSwitcher)findViewById(R.id.noteItemVS);
    View switchCheck = switcher.getCurrentView();
    if (switchCheck instanceof EditText) {
        EditText l = (EditText)switchCheck;
        String x = l.getText().toString();
        Log.e("tree",x);
        if (!x.isEmpty()) {
            switcher.showPrevious();
            TextView myTV = (TextView) switcher.findViewById(R.id.noteItemTextView);
            myTV.setText(x);
        }
    }else{
        TextView l = (TextView) switchCheck;
        String x = l.getText().toString();
        switcher.showNext();
        EditText myEditText = (EditText) switcher.findViewById(R.id.noteItemEditText);
        myEditText.setText(x);
    }
}
票数 0
EN

Stack Overflow用户

发布于 2016-07-24 15:04:24

通过摆脱视图开关,我能够解决这个问题。简单地使用View.Gone和View.Visibile

代码语言:javascript
复制
protected void switchViews(NoteItem note) {
        if (context.findViewById(R.id.noteItemTextView).getVisibility()==View.GONE) {
            EditText editText = (EditText) context.findViewById(R.id.noteItemEditText);
            String x  = editText.getText().toString();
            editText.setVisibility(View.GONE);
            TextView textView = (TextView) context.findViewById(R.id.noteItemTextView);
            textView.setVisibility(View.VISIBLE);
        } else {
            TextView textView = (TextView) context.findViewById(R.id.noteItemTextView);
            String x  = textView.getText().toString();
            textView.setVisibility(View.GONE);
            EditText editText = (EditText) context.findViewById(R.id.noteItemEditText);
            editText.setText(x);
            editText.setVisibility(View.VISIBLE);
        }
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38553307

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档