点击时按钮不起作用,代码正确

时间:2013-04-04 17:50:48

标签: java android eclipse button

我尝试过像HelloWorld和Count Down计时器这样的测试文件。我的应用程序上的任何一个按钮似乎都不起作用。这些代码段是我从互联网上下载的代码段,据报道它们可以工作。我不知道问题是什么。我也是非常新的eclipse和android app编程。

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:textColor="#FF0000"
    android:background="#000000"
    android:layout_height="fill_parent"

    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="#FF0000"
    android:text="CountDown Timer Demo"
    />
<Button
    android:text="Seizure Detected"
    android:id="@+id/start"
    android:textColor="#FF0000"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
</Button>
<Button
    android:text="Override"
    android:id="@+id/stop"
    android:textColor="#FF0000"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
</Button>

<Button
    android:text="click me"
    android:id="@+id/Button01"
    android:textColor="#FF0000"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
</Button>
<TextView
    android:id="@+id/tv"
    android:textColor="#FF0000"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:text="sexy"/>

</LinearLayout>

CountDownTest.java&gt;在src文件夹中

package com.example.epilepsytestapp;

import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class CountDownTest extends Activity {
    Button start, stop;
    TextView tv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        start = (Button)this.findViewById(R.id.start);
        stop = (Button)this.findViewById(R.id.stop);
        tv  = (TextView)this.findViewById(R.id.tv);
        tv.setText("10"); // startting from 10.

        final MyCounter timer = new MyCounter(10000,1000);
        start.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                timer.start();
            }
        });
        stop.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                timer.cancel();
            }
        });
    }

    public class MyCounter extends CountDownTimer{

        public MyCounter(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {
            System.out.println("Timer Completed.");
            tv.setText("Timer Completed.");
        }

        @Override
        public void onTick(long millisUntilFinished) {
            tv.setText((millisUntilFinished/1000)+"");
            System.out.println("Timer  : " + (millisUntilFinished/1000));
        }
    }
}

这是Helloworld.java

package com.example.epilepsytestapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class HelloWorld extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) this.findViewById(R.id.Button01);
        button.setOnClickListener(new OnClickListener() {
           //@Override
           public void onClick(View v) {
            Toast.makeText(HelloWorld.this, "Hello World", Toast.LENGTH_SHORT).show();
           }
         });        
    }
}

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.epilepsytestapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity android:name=".CountDownTest"/>
        <activity
            android:name="com.example.epilepsytestapp.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" 
                    android:name=".CountDownTest"/>

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

1 个答案:

答案 0 :(得分:0)

试试这个。你好世界代码

package com.example.epilepsytestapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.Button01);
        button.setOnClickListener(new OnClickListener() {
           @Override
           public void onClick(View v) {
            Toast.makeText(this, "Hello World", Toast.LENGTH_SHORT).show();
           }
         });        
    }
}