onClick函数在android studio

时间:2016-07-02 17:22:06

标签: android

我试图制作一个计时器应用程序,但我遇到了一个障碍。当我尝试单击按钮时,它不执行任务。 onClick函数似乎在某处出错了。

MainActivity.java

    package toast.akappstudio.com.eggtimer;

    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.SeekBar;
    import android.widget.TextView;

    public class MainActivity extends AppCompatActivity {
        SeekBar timerSeekBar;
        TextView timerTextView;
        public void updateTimer(int secondsLeft){

            int minutes = (int) secondsLeft / 60;
            int seconds = secondsLeft - minutes * 60;




            timerTextView.setText(Integer.toString(minutes) + ":" + Integer.toString(seconds));

        }


        public void controlTimer(View view){

            Log.i("Work","Yes");

          /*  new CountDownTimer(timerSeekBar.getProgress() * 1000, 1000){

               @Override
               public void onTick(long millisUntilFinished) {

                   updateTimer((int) millisUntilFinished / 1000);

               }

               @Override
               public void onFinish() {
                   Log.i("Finished", "Done");
               }
           }.start();
    */

        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            timerSeekBar = (SeekBar)findViewById(R.id.timerSeekBar);
            timerTextView = (TextView) findViewById(R.id.timerTextView);



            timerSeekBar.setMax(600);
            timerSeekBar.setProgress(30);


            timerSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                @Override
                public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

                    updateTimer(progress);
                }

                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {

                }

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {

                }
            });


        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_main, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();

            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }

            return super.onOptionsItemSelected(item);
        }
    }

content.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="toast.akappstudio.com.eggtimer.MainActivity"
    tools:showIn="@layout/activity_main">

    <SeekBar
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/timerSeekBar"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:indeterminate="false" />

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/imageView"
        android:src="@drawable/egg"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginBottom="21dp"
        android:layout_below="@+id/timerSeekBar" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0:30"
        android:id="@+id/timerTextView"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:singleLine="true"
        android:textSize="70sp"
        android:onClick="controlTimer" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="GO!"
        android:id="@+id/controllerButton"
        android:layout_marginTop="55dp"
        android:layout_below="@+id/timerTextView"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

logcat的

07-02 13:11:32.074 1999-1999/? D/dalvikvm: Late-enabling CheckJNI
07-02 13:11:32.170 1999-1999/toast.akappstudio.com.eggtimer W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;)
07-02 13:11:32.170 1999-1999/toast.akappstudio.com.eggtimer I/dalvikvm: Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.view.WindowCallbackWrapper.onSearchRequested
07-02 13:11:32.170 1999-1999/toast.akappstudio.com.eggtimer W/dalvikvm: VFY: unable to resolve interface method 17962: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z
07-02 13:11:32.170 1999-1999/toast.akappstudio.com.eggtimer D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
07-02 13:11:32.170 1999-1999/toast.akappstudio.com.eggtimer I/dalvikvm: Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.view.WindowCallbackWrapper.onWindowStartingActionMode
07-02 13:11:32.170 1999-1999/toast.akappstudio.com.eggtimer W/dalvikvm: VFY: unable to resolve interface method 17966: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
07-02 13:11:32.170 1999-1999/toast.akappstudio.com.eggtimer D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
07-02 13:11:32.250 1999-1999/toast.akappstudio.com.eggtimer I/dalvikvm: Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.widget.TintTypedArray.getChangingConfigurations
07-02 13:11:32.250 1999-1999/toast.akappstudio.com.eggtimer W/dalvikvm: VFY: unable to resolve virtual method 421: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
07-02 13:11:32.250 1999-1999/toast.akappstudio.com.eggtimer D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
07-02 13:11:32.250 1999-1999/toast.akappstudio.com.eggtimer I/dalvikvm: Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.widget.TintTypedArray.getType
07-02 13:11:32.250 1999-1999/toast.akappstudio.com.eggtimer W/dalvikvm: VFY: unable to resolve virtual method 443: Landroid/content/res/TypedArray;.getType (I)I
07-02 13:11:32.250 1999-1999/toast.akappstudio.com.eggtimer D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
07-02 13:11:32.278 1999-1999/toast.akappstudio.com.eggtimer D/dalvikvm: GC_FOR_ALLOC freed 174K, 7% free 3355K/3608K, paused 3ms, total 3ms
07-02 13:11:32.278 1999-1999/toast.akappstudio.com.eggtimer I/dalvikvm-heap: Grow heap (frag case) to 5.590MB for 2349444-byte allocation
07-02 13:11:32.286 1999-2008/toast.akappstudio.com.eggtimer D/dalvikvm: GC_FOR_ALLOC freed 4K, 5% free 5645K/5904K, paused 6ms, total 6ms
07-02 13:11:32.426 1999-1999/toast.akappstudio.com.eggtimer D/libEGL: loaded /system/lib/egl/libEGL_genymotion.so
07-02 13:11:32.454 1999-1999/toast.akappstudio.com.eggtimer D/libEGL: loaded /system/lib/egl/libGLESv1_CM_genymotion.so
07-02 13:11:32.458 1999-1999/toast.akappstudio.com.eggtimer D/libEGL: loaded /system/lib/egl/libGLESv2_genymotion.so
07-02 13:11:32.502 1999-1999/toast.akappstudio.com.eggtimer W/EGL_genymotion: eglSurfaceAttrib not implemented
07-02 13:11:32.506 1999-1999/toast.akappstudio.com.eggtimer E/OpenGLRenderer: Getting MAX_TEXTURE_SIZE from GradienCache
07-02 13:11:32.506 1999-1999/toast.akappstudio.com.eggtimer E/OpenGLRenderer: MAX_TEXTURE_SIZE: 16384
07-02 13:11:32.510 1999-1999/toast.akappstudio.com.eggtimer E/OpenGLRenderer: Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
07-02 13:11:32.514 1999-1999/toast.akappstudio.com.eggtimer E/OpenGLRenderer: MAX_TEXTURE_SIZE: 16384
07-02 13:11:32.514 1999-1999/toast.akappstudio.com.eggtimer D/OpenGLRenderer: Enabling debug mode 0

在此之后,当我尝试在模拟器中运行应用程序并单击“转到”按钮时,它不会在日志中显示任何内容。

3 个答案:

答案 0 :(得分:2)

您正在TextView上设置事件处理程序而不是Button

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0:30"
        android:id="@+id/timerTextView"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:singleLine="true"
        android:textSize="70sp"
        android:onClick="controlTimer" />

将其设置在Button

答案 1 :(得分:0)

您还没有在代码中为按钮设置任何单击侦听器,或者为了简洁起见,您是否未在代码中包含代码?但是,如果情况并非如此,那么您可以在活动中通过两种方式在按钮上设置点击监听器:

方法1

@Override
protected void onCreate(Bundle savedInstanceState) {
   ......

   Button controllerButton = (Button) findViewById(R.id.controllerButton);
   controllerButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Code you want to execute on click of a button
        }
    });
}

方法2

<强> content.xml中

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="GO!"
    android:id="@+id/controllerButton"
    android:layout_marginTop="55dp"
    android:layout_below="@+id/timerTextView"
    android:layout_centerHorizontal="true"
    android:onClick="onClick" />

<强> MainActivity.java

public void onClick(View view) {
   switch(view.getId()) {
      case R.id.controllerButton:
           //Code you want to execute on click of a button
           break;
   }
}

答案 2 :(得分:0)

错误原因是您的按钮代码。只要您在没有android:onClick=""的xml布局中创建按钮,就无法在java文件中使用onClick(View)。但是,您仍然可以将controlTimer(View)方法替换为以下代码段:

,从而使其工作正常
Button btn = (Button)this.findViewById(R.id.controlTimer);

btn.setOnClickListener(new View.OnClickListener(){

new CountDownTimer(timerSeekBar.getProgress() * 1000, 1000){

           @Override
           public void onTick(long millisUntilFinished) {

               updateTimer((int) millisUntilFinished / 1000);

           }

           @Override
           public void onFinish() {
               Log.i("Finished", "Done");
           }
       }.start();
});