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

问题描述 投票:0回答:2

我将字符串数组链接到一个变量,我有一个按钮,它将索引加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>

预期:应该在按钮上逐个设置消息,实际:但没有任何反应

java android
2个回答
2
投票

原因是你错过了将监听器设置为你的按钮。

btn_message.setOnClickListener(this);

0
投票

正如@John Joe提到的那样,如果你不需要实现监听器就错过了将监听器设置为你的按钮在xml的按钮中添加android:onClick="onClick"

<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:onClick="onClick"
    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" />
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.