尝试从其他布局调用时,不会调用setContentView

时间:2016-08-06 22:28:34

标签: java android android-studio

所以我正在尝试切换布局以获得一个非常简单的2页应用程序。 但是,当我单击我的后退按钮时,setContentView不起作用。 任何人都知道为什么以及如何解决这个问题?

这是我的代码:

 public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button infoBtn = (Button) findViewById(R.id.infoBtn);
        infoBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setContentView(R.layout.activity_info);
            }
        });

        LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View rootView = vi.inflate(R.layout.activity_info, null, true);

        Button backBtn = (Button)rootView.findViewById(R.id.backBtn);
        backBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setContentView(R.layout.activity_main);
                System.out.println("Fired!");
            }
        });
    }
}

1 个答案:

答案 0 :(得分:3)

调用#include <Elementary.h> struct progresses { Evas_Object *moving; Evas_Object *status; }; struct progress_with_value { Evas_Object *progress; double value; }; static void* progress_setter_async(void *data) { struct progress_with_value *pv = data; elm_progressbar_value_set(pv->progress, pv->value); return NULL; } static void some_job_cb(void *data, Ecore_Thread *thread) { int i=0; struct progresses *p = data; struct progress_with_value pv_moving = {p->moving, 0.0}; struct progress_with_value pv_status = {p->status, 0.0}; ecore_main_loop_thread_safe_call_sync(progress_setter_async, &pv_moving); ecore_main_loop_thread_safe_call_sync(progress_setter_async, &pv_status); while (i++<=10) { usleep(200000); pv_moving.value = i * 0.1; ecore_main_loop_thread_safe_call_sync(progress_setter_async, &pv_moving); } pv_status.value = 1.0; ecore_main_loop_thread_safe_call_sync(progress_setter_async, &pv_status); } static void on_clicked_cb(void *data, Evas_Object *obj, void *event_info) { ecore_thread_run(some_job_cb, NULL, NULL, data); } int main(int argc, char* argv[]) { Evas_Object *win; elm_init(argc, argv); elm_policy_set(ELM_POLICY_QUIT, ELM_POLICY_QUIT_LAST_WINDOW_CLOSED); win = elm_win_add(NULL, "sample", ELM_WIN_BASIC); elm_win_title_set(win, "Sample"); elm_win_autodel_set(win, EINA_TRUE); evas_object_resize(win, 800, 600); evas_object_show(win); Evas_Object *box = elm_box_add(win); evas_object_resize(box, 800, 600); evas_object_show(box); Evas_Object *btn = elm_button_add(win); elm_object_text_set(btn, "Start Progress 0.0 to 1.0"); evas_object_show(btn); elm_box_pack_end(box, btn); Evas_Object *wheel = elm_progressbar_add(win); elm_object_style_set(wheel, "wheel"); elm_progressbar_pulse_set(wheel, EINA_TRUE); elm_progressbar_pulse(wheel, EINA_TRUE); evas_object_show(wheel); elm_box_pack_end(box, wheel); struct progresses p; p.moving = elm_progressbar_add(win); p.status = elm_progressbar_add(win); evas_object_show(p.moving); evas_object_show(p.status); evas_object_size_hint_align_set(p.moving, EVAS_HINT_FILL, 0.5); evas_object_size_hint_align_set(p.status, EVAS_HINT_FILL, 0.5); elm_box_pack_end(box, p.moving); elm_box_pack_end(box, p.status); evas_object_smart_callback_add(btn, "clicked", on_clicked_cb, &p); elm_run(); elm_shutdown(); return 0; } 不会创建您的应用,因此您放入SetContentView()的所有代码都不会第二次调用。

这就是人们使用onCreate()Activity s

的原因

您可以封装您的oncreate代码,并在回忆Fragment

时调用它们