Fórum Pegar valores de EditText em Abas diferentes #451110
06/08/2013
0
Frederico Brigatte***
Curtir tópico
+ 0Posts
06/08/2013
Frederico Brigatte***
mostrar num toast EditText da aba A + EditTextSobrenome da aba B
Gostei + 0
07/08/2013
Frederico Brigatte***
Gostei + 0
07/08/2013
Frederico Brigatte***
Obs.: Estou TENTANDO fazer. Peço ajuda nesse erro, por favor. Fico no aguardo.
package com.example.androidtablayout;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class VideosActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.videos_layout);
final EditText Nome = (EditText) findViewById(R.id.edtNome);
Button Salvar = (Button) findViewById(R.id.btnSalvar);
Salvar.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String nome = Nome.getText().toString();
Toast.makeText(getBaseContext(), nome, Toast.LENGTH_LONG).show();
}
});
}
}
Gostei + 0
07/08/2013
Frederico Brigatte***
Gostei + 0
07/08/2013
Deivison Melo
http://www.androidbrasilprojetos.org/android/apostila-de-desenvolvimento-android/
ABração e bons códigos!!!
Gostei + 0
07/08/2013
Frederico Brigatte***
Gostei + 0
08/08/2013
Deivison Melo
OU vc está querendo alguém que faça o código pra vc?
Gostei + 0
08/08/2013
Frederico Brigatte***
Gostei + 0
08/08/2013
Frederico Brigatte***
Eu consegui fazer mas não estou entendendo uma coisa. No final do email explico.
Criei essa classe:
public class MySingleton {
private int myInt;
private String myString;
private static MySingleton instance ;
private MySingleton() {
myInt = 0;
myString = "";
}
public static MySingleton getInstance () {
if ( MySingleton.instance == null ) {
MySingleton.instance = new MySingleton();
}
return MySingleton.instance;
}
public int getMyInt() {
return myInt;
}
public void setMyInt(int i) {
this.myInt = i;
}
public String getMyString() {
return myString;
}
public void setMyString(String s) {
this.myString = s;
}
}
Chamo ela desse jeito, uso isso na 1ª aba:
MySingleton mDados = MySingleton.getInstance ();
mDados.setMyInt(100);
mDados.setMyString("Testando classe");
E uso desse jeito na 3ª:
Salvar.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
MySingleton mDados = MySingleton.getInstance ();
int i = mDados.getMyInt();
String s = mDados.getMyString();
Toast.makeText(getBaseContext(), s, Toast.LENGTH_LONG).show();
}
});
Desse jeito se clico no botão da 3ª aba mostra o texto no toast: Testando classe”
Se mudo para esse jeito na 1ª não mostra nada no Toast:
String s = nome.getText().toString(); mDados.setMyString(s);
Pergunta: O que está errado?
Obs.: PESQUISEI, ENCONTREI E ADAPTEI, só não está funcionando direito. Poderiam me ajudar agora, por favor? Pode ser dúvida de outras pessoas.
Gostei + 0
08/08/2013
Frederico Brigatte***
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
</TabHost>
Main,java
package com.example.androidtablayout;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.Toast;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
public class AndroidTabLayoutActivity extends TabActivity implements OnTabChangeListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabHost = getTabHost();
// Tab for Photos
TabSpec photospec = tabHost.newTabSpec("Photos");
photospec.setIndicator("Photos", getResources().getDrawable(R.drawable.icon_photos_tab));
Intent photosIntent = new Intent(this, PhotosActivity.class);
photospec.setContent(photosIntent);
// Tab for Songs
TabSpec songspec = tabHost.newTabSpec("Songs");
// setting Title and Icon for the Tab
songspec.setIndicator("Songs", getResources().getDrawable(R.drawable.icon_songs_tab));
Intent songsIntent = new Intent(this, SongsActivity.class);
songspec.setContent(songsIntent);
// Tab for Videos
TabSpec videospec = tabHost.newTabSpec("Videos");
videospec.setIndicator("Videos", getResources().getDrawable(R.drawable.icon_videos_tab));
Intent videosIntent = new Intent(this, VideosActivity.class);
videospec.setContent(videosIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(photospec); // Adding photos tab
tabHost.addTab(songspec); // Adding songs tab
tabHost.addTab(videospec); // Adding videos tab
}
public void onTabChanged(String tabId) {
if (tabId.equals("tab_test2")) {
Activity currentActivity = getCurrentActivity();
// aqui recupere o valor do singleton
} // fim if
}
}
Classe MySingleton
package com.example.androidtablayout;
public class MySingleton {
private int myInt;
private String myString;
private static MySingleton instance ;
private MySingleton() {
myInt = 0;
myString = "";
}
public static MySingleton getInstance () {
if ( MySingleton.instance == null ) {
MySingleton.instance = new MySingleton();
}
return MySingleton.instance;
}
public int getMyInt() {
return myInt;
}
public void setMyInt(int i) {
this.myInt = i;
}
public String getMyString() {
return myString;
}
public void setMyString(String s) {
this.myString = s;
}
}
photos_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Screen Design for Photos -->
<TextView android:text="PHOTOS HERE"
android:padding="15dip"
android:textSize="18dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/edtNome"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" >
<requestFocus />
</EditText>
<Button
android:id="@+id/btnSalvar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
PhotosActivity.java (Aba 1)
package com.example.androidtablayout;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class PhotosActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.photos_layout);
final EditText nome = (EditText) findViewById(R.id.edtNome);
Button Salvar = (Button) findViewById(R.id.btnSalvar);
MySingleton mDados = MySingleton.getInstance ();
mDados.setMyInt(100);
mDados.setMyString("Testando classe");
// String s = nome.getText().toString();
// mDados.setMyString(s);
Salvar.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String valor = nome.getText().toString();
Toast.makeText(getBaseContext(), valor, Toast.LENGTH_LONG).show();
}
});
}
}
videos_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Screen Design for VIDEOS -->
<TextView android:text="VIDEOS HERE"
android:padding="15dip"
android:textSize="18dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/edtNome"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" >
<requestFocus />
</EditText>
<Button
android:id="@+id/btnSalvar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
VideosActivity.java - (Aba 3) Aba 2 códigos xml e java
package com.example.androidtablayout;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class VideosActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.videos_layout);
final EditText Nome = (EditText) findViewById(R.id.edtNome);
Button Salvar = (Button) findViewById(R.id.btnSalvar);
Salvar.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// String nome = Nome.getText().toString();
// Toast.makeText(getBaseContext(), "teste", Toast.LENGTH_LONG).show();
MySingleton mDados = MySingleton.getInstance ();
int i = mDados.getMyInt();
String s = mDados.getMyString();
Toast.makeText(getBaseContext(), s, Toast.LENGTH_LONG).show();
}
});
}
}
Gostei + 0
13/08/2013
Diogo Souza
Achei um dúvida bem interessante... vamos aos poucos para ver se entendo o cenário...
Você tenta recuperar valores de locais distintos em uma mesma Activity... Já tentou só recuperar o objeto pelo id e chamar o respectivo getText()? Caso aconteça uma exception, tentou tornar as variáveis estáticas?
Gostei + 0
14/08/2013
Frederico Brigatte***
Gostei + 0
14/08/2013
Frederico Brigatte***
Gostei + 0
14/08/2013
Diogo Souza
Primeiro preciso saber se você testou criar as variáveis referentes aos objetos EditText como estáticas...?
Tentou?
Gostei + 0
14/08/2013
Frederico Brigatte***
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)