Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android ViewSwitcher不转到上一个视图_Android_Xml_Viewswitcher - Fatal编程技术网

Android ViewSwitcher不转到上一个视图

Android ViewSwitcher不转到上一个视图,android,xml,viewswitcher,Android,Xml,Viewswitcher,您好,我正在尝试使用视图切换器在和文本视图和编辑文本之间来回切换。我的视图从textview切换到edittext。但不会切换回来。这是我的xml: <ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/noteItemVS" android:layout_width="match_parent"

您好,我正在尝试使用
视图切换器
在和
文本视图
编辑文本
之间来回切换。我的视图从
textview
切换到
edittext
。但不会切换回来。这是我的
xml

<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>

通过去掉视图切换器,我能够解决这个问题。只需使用View.Gone和View.visible

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);
        }
    }

通过去掉视图切换器,我能够解决这个问题。只需使用View.Gone和View.visible

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);
        }
    }

之所以会发生这种情况,是因为解释器同时执行if语句,默认情况下后者重写了前面语句的值。 试试下面的代码,它肯定会工作

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);
    }
}

之所以会发生这种情况,是因为解释器同时执行if语句,默认情况下后者重写了前面语句的值。 试试下面的代码,它肯定会工作

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);
    }
}

您还可以使用ViewPager,它在页面之间也有转换,不像简单视图。可见和视图。GoE实现您还可以使用ViewPager,它在页面之间也有转换,不像简单视图。可见和视图。GoE实现如果您真的想要,您仍然可以使用View switcher,试着找出问题所在。查看答案如果您真的愿意,您仍然可以使用view switcher,只需尝试找出问题所在。看到答案了吗