setContentView和侦听器 [英] setContentView and Listeners

查看:89
本文介绍了setContentView和侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有时会更改Activity的ContentView. (至View2). 将其更改回View1后,侦听器将不再工作. 我已经尝试过将Listener放在onResume()方法中.

I'm changing my ContentView of the Activity at some point. (to View2). After changing it back to View1, the listeners are not more working. I already tried putting the Listener in the onResume() method.

使用setContentView()来显示例如进度屏幕/请等待,...(正在运行asyncTask时). 还是每个活动只有一个mainView? (并动态替换内容).

Is it common anyway to use setContentView() to display e.g. a Progress screen/please wait,...(while an asyncTask is running). Or should you only have ONE mainView for each Activity? (and replacing the content dynamically).

//具体来说: 我正在寻找类似的东西

// To be more specific: I am looking for something like

LinearLayout item = (LinearLayout) findViewById(R.id.mainView);
View child = getLayoutInflater().inflate(R.layout.progress, null);
item.addView(child);

但不应添加"progress.xml",而应删除当前布局并仅显示"progress.xml". 我需要一个容器"并显示/隐藏mainView/progress吗? 但这对我来说似乎不太合适...

but instead of adding the "progress.xml", it should remove the current layout and ONLY show "progress.xml". Do I need an "container" and show/hide mainView/progress? But that doesn't seem very proper to me...

另请参见下面的代码(已剥离)

See also code below (stripped)

public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.view1);

       button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                doSomething();
            }
        });
}

setContentView(R.layout.view2);
[...]
setContentView(R.layout.view1);

//Listener not more working

推荐答案

感谢大家的答复.您让我意识到,当我删除或替换(使用setContentView())主视图时,onClickListeners会迷路.我现在这样结束了:

Thank you all, for your reply. You made me realize, the onClickListeners are getting lost when I remove or replace (using setContentView()) the main view. I ended up this way now:

onCreate:

setContentView(R.layout.parse);
LinearLayout container = (LinearLayout) findViewById(R.id.container);
container.addView(getLayoutInflater().inflate(R.layout.dialog, null));
container.addView(getLayoutInflater().inflate(R.layout.progress, null));

onStartDoingSomething:

onStartDoingSomething:

findViewById(R.id.dialog).setVisibility(View.INVISIBLE);
findViewById(R.id.progress).setVisibility(View.VISIBLE);

onEndDoingSomehting:

onEndDoingSomehting:

findViewById(R.id.dialog).setVisibility(View.VISIBLE);
findViewById(R.id.progress).setVisibility(View.INVISIBLE);

我可能会像nmr所说的那样将View.INVISIBLE更改为View.GONE,但是由于我从未使用过View.GONE,因此我必须先检查Android doku;)

I might change View.INVISIBLE to View.GONE, like nmr said, but since I have never used View.GONE, I have to check the Android doku first ;)

这篇关于setContentView和侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆