2016-07-24 31 views
1

您好,我正在尝试使用viewswitcher来回之间和TextViewEditText。我的视图从textview切换到edittext。但不转回。这里是我的xmlViewSwitcher不会去前视图

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

你也可以使用ViewPager,它也不同于简单View.Visible和View.Gone实施 –

回答

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

你仍然可以使用视图切换,如果你真的想要的,只是试图找出问题所在,其中页面之间的转换。看到答案 –