不同的布局在同一个活动 [英] Different layouts in the same activity
问题描述
对不起,我的英语水平。我工作的一个Android应用程序这是第一个活动有一个密码文本框。根据不同的密码,应用程序进行到一个especific活性。 我的问题了,我有几个活动开,但我认为它更有效的只有一个活动和一些布局,然后,根据不同的密码,应用程序打开同一个活动,但蒙山不同的布局蒙山信息。但我不知道该怎么做。
我的code的:
包com.example.museoprueba;
进口android.app.Activity;
进口android.content.Intent;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.EditText;
公共类interfazSq延伸活动{
私人的EditText HOLA;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.interfazsq);
HOLA =(EditText上)findViewById(R.id.hola);
}
公共无效lAcuriosidades(视图v){
。字符串釜= hola.getText()的toString();
如果(clave.equals(cebra)){
意图I =新的意图(这一点,sqCebra.class);
startActivity(ⅰ);
}
否则,如果(clave.equals(美洲虎)){
意图I =新的意图(这一点,sqJaguar.class);
startActivity(ⅰ);
}
}
}
您可以使用片段可动态改变单一的活动不同的布局:
- 扩展的 FragmentActivity ,而不是活动。
- 在活动的布局(interfazsq.xml),加一个任何布局等的LinearLayout,RelativeLyaout ...等
- 在interfazSq类,夸大该布局为一个的ViewGroup
- 使用的 FragmentManager 可动态添加片段成的ViewGroup。您不必使用意图或离开当前的活动为不同的密码情况显示不同的布局。
http://developer.android.com/guide/components/fragments。 HTML#添加
Sorry for my english. I'm working on an Android app which in it first activity has a password textfield. Depending on the password, the app goes to an especific activity. My problem its i have several activities to open, but I think its more effective have only one activity and several layouts,then, depending of the password, the app opens the same activity but whith different layout whith information. But I have no idea how to do that.
My code its:
package com.example.museoprueba;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class interfazSq extends Activity {
private EditText hola;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.interfazsq);
hola=(EditText)findViewById(R.id.hola);
}
public void lAcuriosidades(View v) {
String clave=hola.getText().toString();
if (clave.equals("cebra")) {
Intent i=new Intent(this,sqCebra.class);
startActivity(i);
}
else if (clave.equals("jaguar")){
Intent i=new Intent(this,sqJaguar.class);
startActivity(i);
}
}
}
You can use the Fragment to dynamically change different layouts in single Activity:
- Extends FragmentActivity instead of Activity.
- In your Activity's layout (interfazsq.xml), add one of any layout such as LinearLayout, RelativeLyaout...etc
- In your interfazSq class, inflate that layout as a ViewGroup.
- Use FragmentManager to dynamically add Fragment into that ViewGroup. You don't have to use Intent or leave current Activity for displaying different layouts for different password conditions.
http://developer.android.com/guide/components/fragments.html#Adding
这篇关于不同的布局在同一个活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!