2013-02-15 88 views
0

中創建一個編輯模式類與視圖切換器我試圖創建一個編輯模式的頁面,我的意思是,你可以點擊一個按鈕,所有的textview轉換成edittext。所以,我創建了一個自定義視圖此與視圖切換:在android

自定義視圖:

public class EditabeText extends ViewSwitcher{ 

private Context m_context; 
private View m_view; 
private TextView tv; 
private EditText edit; 
public EditabeText(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    m_context = context; 
    lauch(); 
} 
public EditabeText(Context context) { 
    super(context); 
    m_context = context; 
    lauch(); 
} 
public void lauch(){ 
    inflate(); 
    bind(); 
} 
public void inflate(){ 
    LayoutInflater inflater = (LayoutInflater)m_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    m_view = inflater.inflate(R.layout.editable_text_view, this); 
} 
public void bind(){ 
    tv = (TextView) m_view.findViewById(R.id.tv_editable_text); 
    edit = (EditText) m_view.findViewById(R.id.edit_editable_text); 
} 
public void init(){ 

} 
public void showEditText(){ 
    if(this.getCurrentView() == tv){ 
     this.showNext(); 
     Toast.makeText(m_context, "showing ... edit mode", Toast.LENGTH_SHORT).show(); 
    }else{ 
     Toast.makeText(m_context, "already in tv mode", Toast.LENGTH_SHORT).show(); 
    } 
} 
public void showTextView(){ 
    if(this.getCurrentView() == edit){ 
     this.showPrevious(); 
     Toast.makeText(m_context, "showing ... tv mode", Toast.LENGTH_SHORT).show(); 
    }else{ 
     Toast.makeText(m_context, "already in edit mode", Toast.LENGTH_SHORT).show(); 
    } 
} 

} 

而這裏的XML:

<ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/switcher" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<TextView 
    android:id="@+id/tv_editable_text" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:text="Text" /> 

<EditText 
    android:id="@+id/edit_editable_text" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:hint="Enter text" /> 

</ViewSwitcher> 

這是我的主要活動:

public class Test2 extends Activity { 
EditabeText edit_tv; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    LinearLayout ll_layout = new LinearLayout(getApplicationContext()); 
    // 
    edit_tv = new EditabeText(getApplicationContext()); 
    // 
    Button button = new Button(getApplicationContext()); 
    button.setText("change"); 
    button.setOnClickListener(listener); 
    // 
    ll_layout.addView(edit_tv); 
    ll_layout.addView(button); 
    // 
    setContentView(ll_layout); 
} 
public OnClickListener listener = new OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     Toast.makeText(getApplicationContext(), "click", Toast.LENGTH_SHORT).show(); 
     edit_tv.showEditText(); 
    } 
}; 
} 

但它表示「已處於電視模式」,而TextView是視圖切換器中的第一個視圖。

回答

1

在佈局文件不要使用(R.layout.editable_text_view是你貼什麼我猜的)另一個ViewSwitcher,因爲這會讓你有一個ViewSwitcherEditabeText類),其中有單個子視圖,另一個ViewSwitcher有兩個兒童意見(TextViewEditText)。在這種情況下,顯示TextViewEditText將始終失敗,因爲this.getCurrentView() == tv(或== edit)將匹配包裝ViewSwitcherTextViewEditText。像這樣改變你的佈局:

<merge xmlns:android="http://schemas.android.com/apk/res/android"> 
<TextView 
    android:id="@+id/tv_editable_text" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:text="Text" /> 

<EditText 
    android:id="@+id/edit_editable_text" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:hint="Enter text" /> 
</merge>