I follow the instruction of this https://developer.android.com/guide/topics/ui/controls/button.html#HandlingEvents in my code of the fragment i have this
public final class Pag1 extends Fragment {
Button buton;
TextView texto;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
// return inflater.inflate(R.layout.fragment_pag1, container, false);
View vista =inflater.inflate(R.layout.fragment_pag1,container,false);
buton= (Button)vista.findViewById(R.id.button);
texto= (TextView) vista.findViewById(R.id.textView2);
buton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
texto.setText("sadfasdfsad");
}
});
return vista;
}
}
I don´t know why this not working, i see more post and not working for me If anywants download my project with this https://mega.nz/#!lAwjSIhC!j0nsG4ilwG9VS6_s1zuiY1Qe_0qXsxl0xpiieqVUCw0
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/scrollView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_alignTop="@+id/button4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<!-- estas dos lineas arreglan el fallo de que el edit text se
volviese con el foco al inicio de la aplicacion-->
<!-- tenia un theme que era el que había puesto en el theme editor pero da problemas y esta mal configurado, lo he
quitado en el spinner-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Elige"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<!--android:layout_width="274dp"-->
<Spinner
android:layout_width="match_parent"
android:layout_height="94dp"
android:id="@+id/string_array"
android:layout_marginTop="46dp"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:entries="@array/string_array" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="haz"
android:id="@+id/textView2"
android:layout_below="@+id/spinner"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="48dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/editText"
android:layout_marginTop="40dp"
android:layout_below="@+id/textView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="143dp"
android:layout_height="wrap_content"
android:text="Calcular"
android:id="@+id/button"
android:layout_marginTop="42dp"
android:layout_below="@+id/editText"
android:background="@color/colorAccent"
android:textColor="@color/abc_search_url_text_selected"
android:layout_alignRight="@+id/editText"
android:layout_alignEnd="@+id/editText"
android:layout_weight="0.14"
android:width="12dp"
android:height="25dp"
android:textSize="25dp" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Info"
android:id="@+id/button4"
android:onClick="info"
android:textColor="@color/abc_search_url_text_selected"
android:background="@color/colorAccent"
android:layout_below="@+id/scrollView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="0dp"
android:layout_marginRight="0dp"
android:layout_gravity="right" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/textView3"
android:layout_below="@+id/linearLayout"
android:layout_alignLeft="@+id/linearLayout"
android:layout_alignStart="@+id/linearLayout"
android:layout_marginTop="62dp"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/linearLayout"
android:layout_alignEnd="@+id/linearLayout" />
</LinearLayout>
</ScrollView>
I think it could be because you added the below properties to the LinearLayout that holds the button and maybe the layout is stealing the focus.. try removing those properties and see if that is true.
android:focusable="true"
android:focusableInTouchMode="true"
public class Pag1 extends Fragment {
Button buton;
TextView texto;
public Pag1(){
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View vista =inflater.inflate(R.layout.fragment_pag1,container,false);
buton= (Button) vista.findViewById(R.id.button);
texto= (TextView) vista.findViewById(R.id.textView2);
buton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
texto.setText("sadfasdfsad");
}
});
return vista;
}
}
Try to add the code into onStart() method.
@Override
public void onStart() {
super.onStart();
View v = getView();
buton= (Button) v.findViewById(R.id.button);
buton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
texto.setText("sadfasdfsad");
}
});
}
Finally i fix it, In my mainactivity class
(when in android studio you select tabbed activity, created this class that contains customviews and the switch that select what fragment shows, i set the onclicklistener in the fragment, example
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (getArguments().getInt(ARG_SECTION_NUMBER)==1){
View rootView = inflater.inflate(R.layout.fragment_pag1, container, false);
buton= (Button)rootView.findViewById(R.id.button);
texto= (TextView)rootView.findViewById(R.id.textView2);
buton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
texto.setText("sadfasdfsad");
}
});
return rootView;
}
else if(getArguments().getInt(ARG_SECTION_NUMBER)==2){
View rootView = inflater.inflate(R.layout.fragment_pag2, container, false);
//
boton = (Button)rootView.findViewById(R.id.main_button);
edit = (EditText)rootView.findViewById(R.id.main_editText);
text = (TextView)rootView.findViewById(R.id.main_textview);
boton.setOnClickListener( new View.OnClickListener() {
public void onClick(View view){
String mensaje = edit.getText().toString();
text.setText(String.valueOf(mensaje.length()));
}
});
//
return rootView;
}
else{
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
//
//
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.