Android ViewFlipper 如何添加重复的子项和子项更改每个中的特定编辑文本
我在 Android 应用程序中设置了一个 ViewFlipper,它将托管一系列窗口来显示消息。每个窗口应该对应一组不同的消息,类似于多个打开的聊天。对于每个窗口,我使用相同的 window.xml 视图将视图带到屏幕上,它还有需要编辑的 EditText 变量。
作为参考,我正在创建和添加子项,如下所示:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
detector = new GestureDetector(this,this);
setContentView(R.layout.viewflip);
flipper = (ViewFlipper) findViewById(R.id.viewflip);
addChild(flipper);
addChild(flipper);
}
private void addChild(ViewFlipper flip){
int index=0;
View view = getView();
if(flip.getChildCount()==0){
flip.addView(view,index);
}
else{
flip.addView(view,flip.getChildCount());
}
}
private View getView(){
LayoutInflater inflater = this.getLayoutInflater();
View view = inflater.inflate(R.layout.window, null);
return view;
}
如您所见,我基本上是在复制视图(我不确定这是否是我的设计的正确方法)。因此,如果我在添加子项后在 onCreate 函数中执行某些操作,例如
EditText messageHistoryText = (EditText) findViewById(R.id.messageHistory);
messageHistoryText.append("Testing :\n");
我在两个窗口上看到文本。
我认为这样的东西会更好:
View v1 = flipper.getChildAt(1);
EditText messageHistoryText2 = (EditText) v1.findViewById(R.id.messageHistory);
messageHistoryText2.append("Testing2 :\n");
但是当我使用那个时,我根本看不到任何东西。也许添加孩子时出现错误。也许我无法使用相同的视图,或者我可能以错误的方式有选择地更改 EditText。
尖端?
I have set up a ViewFlipper in my Android application that will host a series of windows to display messages. Each window should correspond to a different set of messages, similar to multiple open chats. For each window I am using the same window.xml view to bring the view onto the screen, it also has the variable for the EditText need to edit.
For reference I am creating and adding children as follows:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
detector = new GestureDetector(this,this);
setContentView(R.layout.viewflip);
flipper = (ViewFlipper) findViewById(R.id.viewflip);
addChild(flipper);
addChild(flipper);
}
private void addChild(ViewFlipper flip){
int index=0;
View view = getView();
if(flip.getChildCount()==0){
flip.addView(view,index);
}
else{
flip.addView(view,flip.getChildCount());
}
}
private View getView(){
LayoutInflater inflater = this.getLayoutInflater();
View view = inflater.inflate(R.layout.window, null);
return view;
}
As you can see, I am basically duplicating the view (I'm not sure this is the proper approach to my design). So if I were to do something in the onCreate function after adding the children, such as
EditText messageHistoryText = (EditText) findViewById(R.id.messageHistory);
messageHistoryText.append("Testing :\n");
I see the text on both windows.
I thought that something like this would be better:
View v1 = flipper.getChildAt(1);
EditText messageHistoryText2 = (EditText) v1.findViewById(R.id.messageHistory);
messageHistoryText2.append("Testing2 :\n");
but when I use that one, I don't see anything at all. Perhaps there is a mistake on adding the children. Perhaps I can't use the same view, or perhaps I am selectively changing an EditText in an incorrect way.
Tips?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试以下操作,
其中 j 表示 ViewFlipper 中的子位置。
Try the following,
where j represents the child position in ViewFlipper.