1

こんにちは、と のviewswitcher間を行き来するために a を使用しようとしています。私のビューはから に切り替わります。しかし、スイッチバックしません。これが私のものです:TextViewEditTextedittexttextviewxml

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

切り替えるための私のコードは次のとおりです。

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

2 に答える 2

0

これは、インタープリターが両方の if ステートメントを通過し、デフォルトで後者のステートメントが forment ステートメントの値をオーバーライドするために発生します。以下のコードを試してみてください。確実に機能します。

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);
    }
}
于 2016-07-24T17:26:22.690 に答える
0

ビュースイッチャーを取り除くことで、問題を解決できました。View.Gone と View.Visibile を使用するだけで

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);
        }
    }
于 2016-07-24T15:04:24.690 に答える