Вы находитесь на странице: 1из 11

Gua bsica de configuracin de pestaas (tabhost)

-Primero tenemos que crear el contenido xml sobre el activity_main.xml





















Codigo XML del activity
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="match_parent"
android:layout_width="match_parent">

<TabHost android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TabWidget android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@android:id/tabs" />

<FrameLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@android:id/tabcontent" >

<LinearLayout android:id="@+id/tab1"
android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent" >
<TextView android:id="@+id/textView1"
android:text="Contenido Tab 1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

<LinearLayout android:id="@+id/tab2"
android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent" >
<TextView android:id="@+id/textView2"
android:text="Contenido Tab 2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

</FrameLayout>

</LinearLayout>

</TabHost>

</LinearLayout>



Codigo Java

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.app.Activity;
import android.content.res.Resources;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;


public class MainActivity extends ActionBarActivity {

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

Resources res = getResources();
TabHost tabs=(TabHost)findViewById(android.R.id.tabhost);
tabs.setup();

TabHost.TabSpec spec=tabs.newTabSpec("mitab1");
spec.setContent(R.id.tab1);
spec.setIndicator("TAB1",
res.getDrawable(android.R.drawable.ic_dialog_info));
tabs.addTab(spec);

spec=tabs.newTabSpec("mitab2");
spec.setContent(R.id.tab2);
spec.setIndicator("TAB2",
res.getDrawable(android.R.drawable.ic_dialog_map));
tabs.addTab(spec);

tabs.setCurrentTab(0);

tabs.setOnTabChangedListener(new OnTabChangeListener() {
public void onTabChanged(String tabId) {
Log.i("AndroidTabsDemo", "Pulsada pestaa: " +
tabId);
}
});
}




@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;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}





Teniendo en cuenta ese ejemplo procedemos a crear otro ms completo

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="match_parent"
android:layout_width="match_parent">

<TabHost android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TabWidget android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@android:id/tabs" />

<FrameLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@android:id/tabcontent" >

<LinearLayout android:id="@+id/tab1"
android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hola esto es una prueba" />

</LinearLayout>

<LinearLayout android:id="@+id/tab2"
android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent" >

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ingrese un numero:"

android:textAppearance="?android:attr/textAppearanceLarge" /> />

<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >

<requestFocus />
</EditText>

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ingrese otro numero"

android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"

android:textAppearance="?android:attr/textAppearanceLarge" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sumar"
android:onClick="suma" />

</LinearLayout>

</FrameLayout>

</LinearLayout>

</TabHost>

</LinearLayout>






















Interfaz Grafica






































Codigo java

package com.ugb.pestanas;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.app.Activity;
import android.content.res.Resources;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity {
public double V1,V2;
public EditText qv1,qv2;
public TextView r;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
qv1 = (EditText)findViewById(R.id.editText1);
qv2= (EditText)findViewById(R.id.editText2);
r = (TextView)findViewById(R.id.textView4);

Resources res = getResources();
TabHost tabs=(TabHost)findViewById(android.R.id.tabhost);
tabs.setup();

TabHost.TabSpec spec1=tabs.newTabSpec("mitab1");
TabHost.TabSpec spec2=tabs.newTabSpec("mitab2");


spec1.setContent(R.id.tab1);
spec1.setIndicator("restar",
res.getDrawable(android.R.drawable.ic_dialog_info));
tabs.addTab(spec1);
spec2.setContent(R.id.tab1);

//aqui agregamos otra pestaa
spec2=tabs.newTabSpec("mitab2");
spec2.setContent(R.id.tab2);
spec2.setIndicator("sumar",
res.getDrawable(android.R.drawable.ic_dialog_map));
tabs.addTab(spec2);

tabs.setCurrentTab(0);

tabs.setOnTabChangedListener(new OnTabChangeListener() {
public void onTabChanged(String tabId) {
Log.i("AndroidTabsDemo", "Pulsada pestaa: " +
tabId);
}
});
}


public void suma(View suma){
V1 = Double.parseDouble(qv1.getText().toString());
V2 = Double.parseDouble(qv2.getText().toString());
r.setText(Double.toString(V1+V2));
}

@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;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}





































El programa como resultado tendr esto:








Ejercicio
Obteniendo ese Resultado Asignar la funcin de resta en la pestaa
Tal y como se muestra en la pestaa suma

Вам также может понравиться