java - resultado - ¿Cómo establecer diferentes cadenas en una vista de texto en Fragmento mientras se hace clic en un botón diferente?
propiedades jbutton java (2)
Soy nuevo en la programación de aplicaciones de Android.
Tengo dos fragmentos: fragmentA.xml y fragmentB.xml .
En fragmentA hay dos botones: Button One (id: btnOne ) y Button Two (id: btnTwo ).
En el fragmentB B hay TextView (id- textView )
En la clase fragmentB java hay dos String - string1 y string2
Ambos botones se refieren a fragmentB .
Quiero dos establecer el texto textView en string1 cuando string1 Button One y string2 cuando string2 botón dos.
¿Cómo puedo hacer eso?
Estaba usando el siguiente código en la clase fragmentB java para mostrar solo string1
public View onCreateView(
LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.oop_page,container,false);
TextView textV = (TextView)rootView.findViewById(R.id.textView);
textV.setText(string1);
return rootView;
}
¿Puedo usar cualquier declaración if-else aquí?
Gracias
FragmentA.java
public boolean btn1Clicked, btn2Clicked = false;
Button button1 = (Button) rootViewfindViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btn1Clicked = true;
}
});
Button button2 = (Button) rootView.findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btn2clicked = true;
}
});
FragmentB.java
public View onCreateView(
LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.oop_page,container,false);
TextView tv1 = (TextView)rootView.findViewById(R.id.textView);
if(!(FragmentA.btn1Clicked))
{
// Not Clicked
}
else{
tv1.setText("Button 1 Clicked!")''
}
return rootView;
}
Crea una clase Global en tu proyecto y define una cadena pública .
public class Global{
public static String TEXT = "";
}
En tu Fragment A cambia este TEXT en el botón clic evento.
but1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Global.TEXT = "string 1";
}
});
but2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Global.TEXT = "string 2";
}
});
lo mismo que en otro botón si tiene
En tu FragmentB simplemente setText() .... tomará el cambio de valor de lase en Text
text.setText(Global.TEXT);