【问题标题】:Button's don't work when clicked on, Code is correct单击时按钮不起作用,代码正确
【发布时间】:2013-04-04 17:50:48
【问题描述】:

我尝试了 HelloWorld 和倒数计时器等测试文件。我的应用程序上的两个按钮似乎都不起作用。这些代码段是我从互联网上下载的,据报道它们可以工作。我不知道可能是什么问题。我也是非常新的 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 >在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>

【问题讨论】:

  • 不起作用是什么意思?应该发生什么?会发生什么?
  • 当我点击模拟器上的按钮时,它们应该可以工作。对于“点击我”按钮,(hello world)它应该输出“hello world”。对于 CountDownTest,当我单击“检测到癫痫发作”按钮时,它应该在电视上从 10 开始一个可见的倒计时计时器。当我单击“覆盖”按钮时,它应该停止计时器。但是,当我单击任何按钮时,什么都没有发生。完全没有。我可以根据需要多次点击它们,但没有任何反应。
  • findViewById之前删除this
  • @user2246076 : 可能你忘记从HelloWorld Activity 启动CountDownTest Activity(如果HelloWorld 是主Activity)然后使用startActivity(new Intent(HelloWorld.this,CountDownTest.class)) 按钮点击HelloWorld Activity。并确保您已将CountDownTest 注册为AndroidManifest.xml 中的活动
  • @user2246076 : 为什么你在 onClick 方法之前评论了//@Override?请取消注释

标签: java android eclipse button


【解决方案1】:

试试这个。你的 hello world 代码

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

【讨论】:

  • 对不起,这似乎也不起作用。那个地方的 MainActivity 甚至没有编译。我的模拟器有问题吗?
  • 你能在模拟器中运行一个新项目(默认有“hello world”)吗?如果您需要mkyong.com/android/android-hello-world-example,请参考此链接
猜你喜欢
  • 2014-03-24
  • 1970-01-01
  • 2023-04-05
  • 1970-01-01
  • 2023-03-04
  • 2017-03-09
相关资源
最近更新 更多