我正在尝试创建一个具有编辑模式的页面,我的意思是,您可以单击按钮,所有 TextView 都会转换为编辑文本。所以我使用 View 切换器为此创建了一个自定义 View :
自定义 View :
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>
这是我的主要 Activity :
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 是 View 切换器内的第一个 View 。
最佳答案
不要在布局文件中使用另一个ViewSwitcher
(R.layout.editable_text_view
是你发布的我猜),因为这会让你有一个ViewSwitcher
(EditabeText 类)有一个 subview ,另一个 ViewSwitcher
有两个 subview (一个 TextView
和一个编辑文本
)。在这种情况下,显示 TextView
或 EditText
将始终失败,因为 this.getCurrentView() == tv
(或 == edit
) 会将包装器 ViewSwitcher
与 TextView
或 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>
关于java - 在android中使用 View 切换器创建编辑模式类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14895976/