2 a8411094 a8411094 于 2014.02.27 21:40 提问

【Android】setContentView和Inflater问题,请大神们帮我解决!大谢~

一个Activity内多个View互相切换,我查的网上的资料都是使用setContentView+LayoutInflater结合操作,比如这段代码。

enter code here

public class MainActivity extends Activity {
View layoutMain = null;
View layoutSecond = null;
Button btn_mainActivity = null;
Button btn_secondActivity;
boolean firstFlag = true;
long time = (long)0.0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater inflater = this.getLayoutInflater();
layoutMain = inflater.inflate(R.layout.activity_main, null);
layoutSecond = inflater.inflate(R.layout.activity_second, null);
setContentView(layoutMain);

    btn_mainActivity  = (Button)findViewById(R.id.btn_mainAc_1);        
    btn_mainActivity.setOnClickListener(new ButtonListen());

}

class ButtonListen implements OnClickListener{

    public void onClick(View v) {
        // TODO Auto-generated method stub
        time = System.currentTimeMillis();
        switch(v.getId()){
        case R.id.btn_mainAc_1:
            Jump2Second();
            break;
        case R.id.btn_secondA_1:
            Jump2Main();
            break;
        default:
            break;
        }
    }

}
public void Jump2Second(){
    setContentView(layoutSecond);
    long currTime = System.currentTimeMillis();
    Toast.makeText(MainActivity.this,
            "时间:"+String.valueOf(currTime-time)+"秒",
            Toast.LENGTH_SHORT).show();
    if(firstFlag){
        btn_secondActivity = (Button)findViewById(R.id.btn_secondA_1);
        btn_secondActivity.setOnClickListener(new ButtonListen());
        firstFlag = false;
    }           

}
public void Jump2Main(){
    setContentView(layoutMain);
    long currTime = System.currentTimeMillis();
    Toast.makeText(MainActivity.this,
            "时间:" +String.valueOf(currTime-time)+"秒",
            Toast.LENGTH_SHORT).show();
}

}

enter code here

但是我自己练习了一个,没有用到Inflater,直接用setcontentview切换,可以吗?和上面的方法有什么不同吗?为什么一定要通过LayoutInflater转换成View,再传到setcontentview中呢,setcontentview不是有一个传ID的重载方法吗?
下面是我的代码:

enter code here

public class MainActivity extends Activity {
private EditText myedit = null;
private Button mybut1 = null;
private Button mybut2 = null;

// private LayoutInflater inflater = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myedit = (EditText) findViewById(R.id.myedit);
    mybut1 = (Button) findViewById(R.id.mybut1);
    mybut1.setOnClickListener(new buttonlisener());
}

public class buttonlisener implements OnClickListener {
    @Override
    public void onClick(View v) {
        switch(v.getId()) {
        case R.id.mybut1:
            setContentView(R.layout.second);
            mybut2 = (Button)MainActivity.this.findViewById(R.id.mybut2);
            mybut2.setOnClickListener(new buttonlisener());
            break;
        case R.id.mybut2:
            setContentView(R.layout.activity_main);
            mybut1 = (Button) findViewById(R.id.mybut1);
            mybut1.setOnClickListener(new buttonlisener());
            break;
        default:
            break;
        }
    }

}

}

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!