Java 在android中使用view switcher创建编辑模式类
我试图创建一个具有编辑模式的页面,我的意思是,你可以点击一个按钮,所有的textview转换成edittext。因此,我使用view switcher为此创建了一个自定义视图: 自定义视图:Java 在android中使用view switcher创建编辑模式类,java,android,xml,viewswitcher,Java,Android,Xml,Viewswitcher,我试图创建一个具有编辑模式的页面,我的意思是,你可以点击一个按钮,所有的textview转换成edittext。因此,我使用view switcher为此创建了一个自定义视图: 自定义视图: public class EditabeText extends ViewSwitcher{ private Context m_context; private View m_view; private TextView tv; private EditText edit; public Editabe
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>
但是当文本视图是视图切换器内的第一个视图时,它会显示“已处于电视模式”。不要在布局文件中使用(
R.layout.editable\u text\u view
我猜是您发布的内容)另一个ViewSwitcher
,因为这将使您拥有一个具有单个子视图的ViewSwitcher
(EditabeText类),另一个ViewSwitcher
,它有两个子视图(一个TextView
和一个EditText
)。在这种情况下,显示文本视图
或编辑文本
将始终失败,因为此.getCurrentView()==tv
(或==edit
)将使包装器视图切换器
与文本视图
或编辑文本
匹配。按如下方式更改布局:
<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>
<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>