片段中的OnClickListener不起作用

时间:2016-09-18 17:09:05

标签: android android-fragments button onclicklistener

我按照https://developer.android.com/guide/topics/ui/controls/button.html#HandlingEvents的说明进行操作 在我的片段代码中我有这个

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

我不知道为什么这不起作用,我看到更多的帖子而不是为我工作 如果有任何人想用这个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>

4 个答案:

答案 0 :(得分:0)

我认为可能是因为您将以下属性添加到包含按钮的LinearLayout,并且可能布局正在窃取焦点。尝试删除这些属性并查看是否为真。

    <?php 
include 'connection.php';
$email = $_POST["email"];

$checkuser = "SELECT * from users where email='$email'";
$checkuserresults = mysql_query($checkuser);    
    $row = mysql_fetch_array($checkuserresults);
if(mysql_num_rows($checkuserresults)){
  echo '<div class="alert alert-dismissible alert-danger">
  <button type="button" class="close" data-dismiss="alert">×</button>
  <strong>SORRY !</strong> Email already exists - TRY ANOTHER EMAIL!!
</div>'; 
}
?>

答案 1 :(得分:0)

//HTML
<table id="item" width="100%" cellspacing="0">
      <thead>
           <tr>
               <th>Name</th>
               <th>Age</th>
               <th>Start date</th>
               <th>Salary</th>
           </tr>
       </thead>
</table>


//JS

var tableData = [
        [   "Tiger Nixon",
            "61",
            "2011/04/25",
            "$320,800"
        ],
        [
           "Garrett Winters",
           "63",
           "2011/07/25",
           "$170,750"
        ],
       [
         "Ashton Cox",
         "66",
         "2009/01/12",
         "$86,000"
       ]
   ]; 

var itemTable = $("#item").DataTable({
     "data":tableData,
     "createdRow": function ( row, data, index ) {

       // you can check value of the any column you want 
       // I have checked the "age" column value
       if(data[1] > 62){
            $(row).attr("data-href", "greaterThanSixtyTwo");
       } else {
            $(row).attr("data-href", "lessThanSixtyTwo");
       }
    }
});   

//click event handler for row 
$('#item tbody').on( 'click', 'tr', function () {
    //fetch the row data
    var rowData = itemTable.row( this ).data();

    //fetch the function to be called on click of this row
    var functionToCall = $(this).data("href");

    //call the function with clicked rows data 
    eval( functionToCall + "( '"+rowData +"' )" );
});

function greaterThanSixtyTwo(rowData){
  console.log(" I am greater than Sixty Two - > : ", rowData);
}

function lessThanSixtyTwo(rowData){
  console.log("I am less than Sixty Two - > : ", rowData);
}

答案 2 :(得分: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");
        }
    });

}

答案 3 :(得分:0)

最后我修复了它,在我的主要活动课

(当你在android studio中选择标签式活动时,创建了这个包含自定义视图的类和选择片段显示的开关,我在片段中设置了onclicklistener,例如

public View onCreateView(LayoutInflater inflater,ViewGroup容器,                                  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;
        }
        //



        //
    }