2016-09-18 110 views
0

我遵循這個https://developer.android.com/guide/topics/ui/controls/button.html#HandlingEvents 指令在我的片段的代碼,我有這個OnClickListener在片段不工作

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

我不知道爲什麼這不工作,我看到更多的崗位,而不是工作我 如果anywants下載我的項目,這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> 
+0

你有沒有得到任何錯誤? –

+0

Nop,我運行的應用程序沒有錯誤,當按下按鈕時,沒有任何反應 – ciberelfo

+0

張貼您的XML佈局以及 –

回答

0

最後我解決這個問題,在我的MainActivity類別

(當在Android Studio中您選擇選項卡活動,創造了這個類,它包含customviews和選擇什麼樣的片段表演,我設置了onclicklistener在開關的片段,例如

公共視圖onCreateView(LayoutInflater充氣,容器的ViewGroup, 捆綁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; 
     } 
     // 



     // 
    } 
0

我認爲這可能是因爲你添加下面的屬性,以保存按鈕,也許佈局偷個的的LinearLayout重點..嘗試刪除這些屬性,看看是否屬實。

android:focusable="true" 
android:focusableInTouchMode="true" 
+0

我刪除它,不工作:(,我將這些行,因爲當我啓動應用程序時,editext得到集中automaticaly – ciberelfo

+0

那好吧嘗試添加這兩個屬性的按鈕,以及也可點擊=「真」 :) – Cata

+0

,並且確保你初始化正確的按鈕ID –

0
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; 
    } 
} 
+0

您只刪除評論行?¿ – ciberelfo

+0

我添加了一個空的構造函數,也刪除了final。 – W4R10CK

+0

在公共類中添加並刪除最終Pag1擴展Fragment並且該按鈕不起作用 – ciberelfo

0

嘗試將代碼添加到在onStart()方法。

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

} 
+0

抱歉回答,無法正常工作 – ciberelfo

+0

@ciberelfo獲取任何錯誤或無效? –

+0

沒有影響,現在我解決它,:D我發佈的解決方案(主要活動onclicklistener) – ciberelfo