java - 由于 ViewPostIME,按钮不执行任何操作

标签 java android

我将字符串数组链接到一个变量,并且我有一个按钮,它将添加索引 1 并将 textView 设置为字符串数组变量 [index],这样我就可以通过以下方式显示字符串数组中的项目列表一个,但是当我按下按钮时,没有任何反应,ViewPostIme0 和 ViewPostIme 指针 1 继续在我的 logcat 中循环

我尝试了一切

我已经尝试将代码简化到最短,但仍然不起作用

简化代码

int index = 0;
Button btn_message;
btn_message = findViewById(R.id.btn_message);

public void onClick(View v) {

        switch (v.getId()) {
            case (R.id.btn_message):

                //if changes in the same page

                index++;
                btn_message.setText(String.valueOf(messages[index]));
}
}

完整代码

package com.example.project;

import android.content.Intent;
import android.content.res.Resources;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    Button btn_message, btn_skip;
    String[] messages, personal_name;
    ImageView LeftImage, RightImage, BackgroundImage;
    String name;
    TextView test, txt_round;
    int index ;
    int character;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Importing values
        final Intent intent2 = getIntent();
        index = intent2.getIntExtra("index", 0);
        Intent intent_name = getIntent();
        name = intent_name.getStringExtra("name");
        //get string array
        personal_name = getResources().getStringArray(R.array.name);
        messages = getResources().getStringArray(R.array.text);
        //find view by id
        btn_message = findViewById(R.id.btn_message);
        LeftImage = findViewById(R.id.img_left);
        RightImage = findViewById(R.id.img_right);
        BackgroundImage = findViewById(R.id.img_Background);
        test = findViewById(R.id.txtx_Test);
        btn_skip = findViewById(R.id.btn_skip);
        txt_round = findViewById(R.id.txt_round);

        String testing = String.valueOf(index);
        test.setText(testing);
        testing = String.valueOf(index);
        test.setText(testing);
        //set Value to varibles
        //if image come to main, use this to change things
        switch (index) {
            case (200):
                //BackgroundImage.setImageResource(R.mipmap.);
                btn_message.setText(String.valueOf(messages[index]));
                break;
        }
           /* case (13):
                test.setText("success");
                btn_message.setText(String.valueOf(messages[index]));
                break;
            case (20):
                test.setText("Success2");
                btn_message.setText(String.valueOf(messages[index]));
                break;

        }*/

        //Intent intent = getIntent();
        //index = intent.getIntExtra("index", 0);

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case (R.id.btn_message):

                //if changes in the same page

                index++;
                String testing = String.valueOf(index);
                test.setText(testing);
                btn_message.setText(String.valueOf(messages[index]));
                switch (index) {
                    //CHANGING POINT ZONE
                    case (8):
                        index = 10;
                        //BackgroundImage.setImageResource(R.mipmap.bg1);
                        BackgroundImage.setScaleType(ImageView.ScaleType.FIT_XY);
                        txt_round.setText(name);
                        break;

                    case (16):
                        index = 20;
                        BackgroundImage.setImageResource(R.mipmap.bg2);
                        BackgroundImage.setScaleType(ImageView.ScaleType.FIT_XY);
                        txt_round.setText(name);
                        break;

                    case (31):
                        index = 40;
                        //BackgroundImage.setImageResource(R.mipmap.bg3);
                        BackgroundImage.setScaleType(ImageView.ScaleType.FIT_XY);
                        txt_round.setText("");
                        break;

                    case (52):
                        index = 60;
                        BackgroundImage.setImageResource(R.mipmap.bg4);
                        BackgroundImage.setScaleType(ImageView.ScaleType.FIT_XY);
                        txt_round.setText("");
                        break;

                    case (66):
                        index = 70;
                        //BackgroundImage.setImageResource(R.mipmap.cg2);
                        BackgroundImage.setScaleType(ImageView.ScaleType.FIT_XY);
                        txt_round.setText(name);
                        break;
                    case (82):
                        index = 90;
                        //BackgroundImage.setImageResource(R.mipmap.bg5);
                        BackgroundImage.setScaleType(ImageView.ScaleType.FIT_XY);
                        txt_round.setText("");
                        break;
                    case (172):
                        index = 180;
                        txt_round.setText(name);
                        BackgroundImage.setImageResource(R.mipmap.bg6);
                        BackgroundImage.setScaleType(ImageView.ScaleType.FIT_XY);
                        break;
                    case (183):
                        index = 190;
                        txt_round.setText(name);
                        //BackgroundImage.setImageResource(R.mipmap.cg3);
                        BackgroundImage.setScaleType(ImageView.ScaleType.FIT_XY);
                        break;


                    //END A END A END A
                    case (114):
                        index = 120;
                        txt_round.setText("");
                        //BackgroundImage.setImageResource(R.mipmap.bg10);
                        BackgroundImage.setScaleType(ImageView.ScaleType.FIT_XY);
                        break;
                    case (126):
                        index = 130;
                        //BackgroundImage.setImageResource(R.mipmap.bg11);
                        BackgroundImage.setScaleType(ImageView.ScaleType.FIT_XY);
                        txt_round.setText(name);
                        break;

                    //END A END A END A
                    case (145):
                        //index = 90;
                        txt_round.setText("");
                        //BackgroundImage.setImageResource(R.mipmap.endA);
                        BackgroundImage.setScaleType(ImageView.ScaleType.FIT_XY);
                        break;
                    //END A END A END A


                    //END B END B END B
                    case (211):
                        index = 220;
                        txt_round.setText(name);
                        //BackgroundImage.setImageResource(R.mipmap.bg6);
                        BackgroundImage.setScaleType(ImageView.ScaleType.FIT_XY);
                        break;
                    case (224):
                        index = 230;
                        txt_round.setText(name);
                        //BackgroundImage.setImageResource(R.mipmap.bg4);
                        BackgroundImage.setScaleType(ImageView.ScaleType.FIT_XY);
                        break;
                    case (234):
                        index = 240;
                        txt_round.setText(name);
                        //BackgroundImage.setImageResource(R.mipmap.cg6);
                        BackgroundImage.setScaleType(ImageView.ScaleType.FIT_XY);
                        break;

                    //END B END B END B
                    case (251):
                        //index = 240;
                        txt_round.setText(name);
                        //BackgroundImage.setImageResource(R.mipmap.cg6);
                        BackgroundImage.setScaleType(ImageView.ScaleType.FIT_XY);
                        break;
                    //END B END B END B


                    //CHOICE AREA
                    case (105):
                        AlertDialog.Builder choice_box = new AlertDialog.Builder(MainActivity.this);

                        View choice_view = getLayoutInflater().inflate(R.layout.activity_choices, null);
                        Button choice1 = choice_view.findViewById(R.id.choice1);
                        Button choice2 = choice_view.findViewById(R.id.choice2);

                        choice_box.setView(choice_view);
                        AlertDialog dialog = choice_box.create();
                        dialog.setCanceledOnTouchOutside(false);

                        final AlertDialog finalDialog3 = dialog;
                        choice1.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                index = 110;
                                btn_message.setText(String.valueOf(messages[index]));
                                txt_round.setText("");
                                finalDialog3.dismiss();
                            }
                        });
                        //if in screen choice, use this to change in different paths
                        final AlertDialog finalDialog2 = dialog;
                        choice2.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                index = 150;
                                //need to set the index 20's value as instant refresting is not working
                                btn_message.setText(String.valueOf(messages[index]));
                                txt_round.setText("");
                                finalDialog2.dismiss();
                            }
                        });

                        dialog.show();

                        Window window = dialog.getWindow();
                        window.setLayout(ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
                        break;


                    case (203):

                        choice_box = new AlertDialog.Builder(MainActivity.this);
                        choice_view = getLayoutInflater().inflate(R.layout.activity_choices, null);
                        choice1 = choice_view.findViewById(R.id.choice1);
                        choice2 = choice_view.findViewById(R.id.choice2);
                        dialog = choice_box.create();


                        final AlertDialog finalDialog = dialog;
                        choice1.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                index = 210;
                                btn_message.setText(String.valueOf(messages[index]));
                                txt_round.setText("");
                                finalDialog.dismiss();
                            }
                        });
                        //if in screen choice, use this to change in different paths
                        final AlertDialog finalDialog1 = dialog;
                        choice2.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                index = 260;
                                //need to set the index 20's value as instant refresting is not working
                                btn_message.setText(String.valueOf(messages[index]));
                                txt_round.setText("");
                                finalDialog1.dismiss();
                            }
                        });

                        dialog.show();

                        window = dialog.getWindow();
                        window.setLayout(ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
                        break;

                    case (264):

                        choice_box = new AlertDialog.Builder(MainActivity.this);
                        choice_view = getLayoutInflater().inflate(R.layout.activity_choices, null);
                        choice1 = choice_view.findViewById(R.id.choice1);
                        choice2 = choice_view.findViewById(R.id.choice2);
                        dialog = choice_box.create();


                        final AlertDialog finalDialog4 = dialog;
                        choice1.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                index = 210;
                                btn_message.setText(String.valueOf(messages[index]));
                                txt_round.setText("");
                                finalDialog4.dismiss();
                            }
                        });
                        //if in screen choice, use this to change in different paths
                        final AlertDialog finalDialog5 = dialog;
                        choice2.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                index = 260;
                                //need to set the index 20's value as instant refresting is not working
                                btn_message.setText(String.valueOf(messages[index]));
                                txt_round.setText("");
                                finalDialog5.dismiss();
                            }
                        });

                        dialog.show();

                        window = dialog.getWindow();
                        window.setLayout(ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
                        break;

                    //CHARCTER ZONE
                    case (23):
                    case (29):
                    case (41):
                    case (49):
                    case (51):
                    case (78):
                    case (91):
                    case (93):
                    case (103):
                    case (111):
                    case (124):
                    case (134):
                    case (158):
                    case (161):
                    case (166):
                    case (169): {//girl
                        txt_round.setText("B");
                        break;
                    }
                    case (63):
                    case (72):
                    case (74):
                    case (152):
                    case (155):
                    case (157):
                    case (160):
                    case (163):
                    case (188):
                    case (171): {//shing
                        character = 2;
                        txt_round.setText("A");
                        break;
                    }

                    case (10):
                    case (20):
                    case (25):
                    case (30):
                    case (70):
                    case (75):
                    case (76):
                    case (79):
                    case (94):
                    case (100):
                    case (104):
                    case (112):
                    case (123):
                    case (130):
                    case (132):
                    case (136):
                    case (139):
                    case (143):
                    case (153):
                    case (159):
                    case (164):
                    case (180):
                    case (191): {//playername
                        txt_round.setText(name);
                        break;
                    }
                    case (193):
                        Intent intent = new Intent(this, ImageActivity.class);

                        intent.putExtra("index", index);
                        startActivity(intent);
                        break;
                    default://noone
                        character = 0;
                        txt_round.setText("");

                        //MINIGAME AREA


                        //OTHER ZONE (PLAYER NAME SET DIALOG ZONE)


                        btn_message.setText(String.valueOf(messages[index]));
                        break;

                    case R.id.btn_skip:


                        break;

                }
        }
    }
}

这是真正需要的主要 .xml 文件

<?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=".MainActivity">

    <ImageView
        android:id="@+id/img_Background"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:scaleType="fitXY"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0"
        app:srcCompat="@mipmap/bg_test" />

    <ImageView
        android:id="@+id/img_right"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="450dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@mipmap/normal" />

    <ImageView
        android:id="@+id/img_left"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginEnd="450dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@mipmap/normal6" />

    <Button
        android:id="@+id/btn_message"
        android:layout_width="713dp"
        android:layout_height="146dp"
        android:background="@mipmap/bar_5"
        android:gravity="top"
        android:lines="5"

        android:minLines="2"
        android:paddingLeft="80px"
        android:paddingTop="50px"
        android:text="「......enq7f…….」"
        android:textAlignment="viewStart"
        android:textSize="26sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/txtx_Test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="TextView"
        android:textColor="@color/colorPrimaryDark"
        android:textSize="36sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/img_Background"

        />

    <TextView
        android:id="@+id/txt_round"
        android:layout_width="171dp"
        android:layout_height="40dp"
        android:layout_marginStart="10dp"
        android:layout_marginEnd="590dp"
        android:background="@mipmap/bar_6"
        android:gravity="center"
        android:paddingTop="15px"
        android:text="\u003F \u003F \u003F"
        android:textSize="24sp"
        app:layout_constraintBottom_toTopOf="@+id/btn_message"
        app:layout_constraintEnd_toEndOf="@+id/img_right"
        app:layout_constraintHorizontal_bias="0.012"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/btn_skip"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_marginStart="600dp"
        android:layout_marginEnd="12dp"
        android:layout_marginBottom="3dp"
        android:background="@mipmap/bar_3"
        android:text="Skip"
        app:layout_constraintBottom_toTopOf="@+id/btn_message"
        app:layout_constraintEnd_toEndOf="@+id/img_right"
        app:layout_constraintHorizontal_bias="0.012"
        app:layout_constraintStart_toStartOf="parent" />

</android.support.constraint.ConstraintLayout>

预期:消息应该被一一设置在按钮上, 实际:但什么也没发生

最佳答案

原因是因为您错过了为按钮设置监听器。

btn_message.setOnClickListener(this);

关于java - 由于 ViewPostIME,按钮不执行任何操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55664023/

相关文章:

java - 使用 JSONObject 放置/获取字节数组值

java - 仅设置拦截器关闭端点

java - 如何避免在 Java 中忙于自旋

java - 保存和查看数据时的时区

android - 软键盘不隐藏

android - 突然得到 android.os.NetworkOnMainThreadException

java - 如何检测字符串中是否存在 URL

java - 尝试在 Xoom(Android 3.0)中从我的应用程序打开 Word 文档,但得到 "Unknown Source"

android - 当我按下 fragment 中的后退按钮时调用哪个生命周期

android - OnDateSetListener() 没有被调用