0

我正在尝试创建一个具有编辑模式的页面,我的意思是,您可以单击一个按钮,然后所有 textview 都将转换为 edittext。所以我用视图切换器为此创建了一个自定义视图:

自定义视图:

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 是视图切换器内的第一个视图。

4

1 回答 1

1

不要在布局文件中使用(R.layout.editable_text_view我猜是你发布的)另一个ViewSwitcher,因为这会让你有一个ViewSwitcherEditabeText类),它有一个子视图,另一个ViewSwitcher有两个子视图(aTextView和 an EditText)。在这种情况下,显示TextVieworEditText将始终失败,因为this.getCurrentView() == tv(or == edit) 会将包装器ViewSwitcherTextViewor匹配EditText。像这样改变你的布局:

<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>
于 2013-02-15T16:28:56.953 回答