点击按钮没有反应

点击按钮没有反应

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context="com.hala.view01.Main2Activity">
 
<RelativeLayout
    android:id="@+id/rl"
    android:layout_width="368dp"
    android:layout_height="439dp"
    android:layout_marginLeft="8dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginRight="8dp"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginTop="8dp"
    android:layout_marginBottom="8dp"
    app:layout_constraintBottom_toTopOf="@+id/button">
 
    <ImageView
        android:id="@+id/iv"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/timg"
        android:visibility="gone"/>
</RelativeLayout>
 
    <Button
        android:onClick="onClick"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="0dp"
        android:text="开始"
        android:id="@+id/button"
        app:layout_constraintHorizontal_weight="1"
        android:layout_marginStart="8dp" />
 
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="分数"
        android:textSize="25sp"
        android:gravity="center"
        app:layout_constraintLeft_toRightOf="@+id/tv"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        app:layout_constraintTop_toTopOf="@+id/button"
        android:layout_marginTop="8dp"
        android:layout_marginRight="8dp"
        app:layout_constraintVertical_bias="0.521"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintHorizontal_weight="1"
        android:layout_marginEnd="8dp" />
 
 
</android.support.constraint.ConstraintLayout>

java文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package com.hala.view01;
 
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
 
import java.lang.ref.WeakReference;
import java.util.Random;
 
public class Main2Activity extends AppCompatActivity implements View.OnClickListener,View.OnTouchListener {
 
 
    public static final int Code = 123;
    public static final int Rand = 500;
    public static final int MAX = 10;
    private TextView tv;
    private Button bt;
    private ImageView iv;
    private RelativeLayout rl;
 
 
    private int total;
    private int success;
 
    private DiglettHandler diglettHandler=new DiglettHandler(this);
 
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
 
        initView();
    }
 
    private void initView() {
        tv = (TextView)findViewById(R.id.tv);
        bt = (Button)findViewById(R.id.button);
        iv = (ImageView)findViewById(R.id.iv);
        rl = (RelativeLayout)findViewById(R.id.rl);
        bt.setOnClickListener(this);
        iv.setOnTouchListener(this);
 
        //此句容易漏掉
        DiglettHandler diglettHandler=new DiglettHandler(this);
 
    }
 
 
    /**
     * 点击按钮开始游戏
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt:
                start();
                break;
        }
    }
 
    /**
     * 开始打第一个地鼠
     * 发送消息
     */
    private void start() {
 
        total=0;
        success=0;
        bt.setText("游戏中。。。");
        bt.setEnabled(false);
 
        next(0);
    }
 
    /**
     * 打第一个以后的地鼠
     * @param delayTime
     */
    private void next(int delayTime){
        //Random().nextInt(368)表示生成0~368的随机数,包括0,不包括368
        int x=new Random().nextInt(368);
        int y=new Random().nextInt(439);
 
        Message message=Message.obtain();
        message.what= Code;
        message.arg1=x;
        message.arg2=y;
 
        diglettHandler.sendMessageDelayed(message,delayTime);
        total++;
    }
 
    /**
     * 游戏结束的反应
     */
    private void clear(){
        total=0;
        success=0;
        iv.setVisibility(View.GONE);
        bt.setText("开始");
        bt.setEnabled(true);
 
    }
 
    /**
     * 打中地鼠后的反应
     * @param v
     * @param event
     * @return
     */
    @Override
    public boolean onTouch(View v, MotionEvent event) {
 
        //地鼠为不可见
        v.setVisibility(View.GONE);
        success++;
        tv.setText("打到"+success+"只,漏掉"+(total-success)+"只哦");
        return false;
    }
 
    public static class DiglettHandler extends Handler{
 
        public final WeakReference<Main2Activity> mWeaker;
 
        public DiglettHandler(Main2Activity activity) {
            mWeaker=new WeakReference<Main2Activity>(activity);
 
        }
 
 
        /**
         * 点击屏幕后的处理机制
         * @param msg
         */
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Main2Activity activity=mWeaker.get();
 
            switch (msg.what){
                case Code:
                    if(activity.total> MAX){
                        activity.clear();
                        activity.tv.setText("^ _ ^ 结束了,下次再来 ^ _ ^");
                        return;
                    }
                    int x=msg.arg1;
                    int y=msg.arg2;
                    activity.iv.setX(x);
                    activity.iv.setY(y);
                    //默认为不显示,这里要显示出来
                    activity.iv.setVisibility(View.VISIBLE);
 
                    //传回随机时间,执行next方法
                    int randomTime=new Random().nextInt(Rand)+ Rand;
                    activity.next(randomTime);
                    break;
            }
        }
    }
}

配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.hala.view01">
 
    <!-- 网络链接,写文件和读文件的权限 -->
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
 
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/title1"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity"></activity>
        <activity android:name=".Main2Activity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
 
</manifest>

程序运行后显示了开始界面但点击开始按钮并没有反应

正在回答

登陆购买课程后可参与讨论,去登陆

3回答

闪退你检查一下as底部的Android Monitor标签,查看一下红色的错误信息是什么?tv设置了显示文本,但你的TextView没有对应tv的id

http://img1.sycdn.imooc.com//climg/5a743be30001984904900263.jpg


  • 慕UI6705487 提问者 #1
    非常感谢!
    2018-02-02 18:46:45
irista23 2018-02-02 17:58:37

http://img1.sycdn.imooc.com//climg/5a7436060001c01c03910180.jpg

这个bt没有在xml找到对应id的Button

  • 提问者 慕UI6705487 #1
    这个问题找到了,但游戏开始后会闪退,而且textview那无法按照编写的内容现实,为什么呢
    2018-02-02 18:02:30
提问者 慕UI6705487 2018-02-02 17:52:01

按钮问题已经解决,但游戏开始一段时间就闪退了,不知道为啥

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
Android网络操作与数据存储2018版
  • 参与学习           人
  • 提交作业       307    份
  • 解答问题       1613    个

本专题是联网及数据处理的必备技能。课程从网络基础知识到线程间协同工作、异步下载处理。介绍了Android内外部文件存储、轻量级数据库SQLite的使用。利用屏幕适配、状态保持、百度地图解决实际问题。

了解课程
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师
插入代码