使用Android的一个微调更改布局 [英] Change layout using a spinner in Android

查看:165
本文介绍了使用Android的一个微调更改布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据我的离心器中选择改变我的行为的布局。
但第一选择后,微调变成白色,我不能够决定另一个选择。
在code我使用的是以下内容:

I'm trying to change the layout of my activity according to my selection inside the spinner. But after the first selection, the spinner become white and I'm not able to decide another selection. The code I'm using is the following:

public class MainActivity extends Activity implements OnItemSelectedListener {

Spinner spinner;
String[] options = { "Modulo1", "Modulo2" };

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

    spinner = (Spinner) findViewById(R.id.spinner);
    ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, options);
    adapter_state
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter_state);
    spinner.setOnItemSelectedListener(this);

}

int check = 0;

public void onItemSelected(AdapterView<?> parent, View view, int position,
        long id) {
    check = check + 1;
    if (check > 1) {
        int selState = spinner.getSelectedItemPosition();
        switch (selState) {
        case 0:
            setContentView(R.layout.activity_main);

            break;
        case 1:
            setContentView(R.layout.activity2_main);

            break;

        }

    }
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

}

//
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

任何建议?
谢谢

Any suggestion? Thanks

推荐答案

你指的是微调您的第一项活动。更换完内容后,微调是不存在了。如果在第二个布局的另一个微调,你必须重新连接并重新设置监听器。
基本上,你有以后每隔的setContentView ...

The spinner you are referring to is in your first activity. After you have replaced the content, the spinner isn't there anymore. If you have another spinner in the second layout, you have to reconnect it and set the listener again. Basically you have to run your onCreate stuff after every setContentView...

作为一个方面说明,无论你正在尝试做的,这可能不是要走的路。要显示另一个完整的布局,更好地利用其他活动。

As a side note, whatever you are trying to do, this is probably not the way to go. To show another full layout, better use another activity.

这篇关于使用Android的一个微调更改布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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