简体   繁体   English

单击另一个按钮后如何启用/禁用按钮?

[英]How to enable/disable button after the other button is clicked?

I have 4 buttons that work like a process which is user need to click button 1 before button 2 is enabled to be click.我有 4 个按钮,它们的工作方式类似于用户需要在按钮 2 启用之前单击按钮 1 才能单击的过程。 To be able to click button 3, a user needs to click button 2 after then the button 3 is able to be a click, the same goes to button 4 which is all the button is enabled to click at the end when user click button 4. I manage to get button 1 and button 2 work but after user click button 2, button 3 is not able to click.为了能够点击按钮 3,用户需要点击按钮 2 之后,按钮 3 才能被点击,按钮 4 也是如此,即当用户点击按钮 4 时,所有按钮最终都可以点击. 我设法让按钮 1 和按钮 2 工作,但是在用户单击按钮 2 后,按钮 3 无法单击。

This is the XML file:这是 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.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:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button2"
        android:layout_width="174dp"
        android:layout_height="41dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="60dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="280dp"
        android:onClick="goToAnotherActivity1"
        android:enabled="true"
        android:text="@string/reconnaissance"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button3"
        android:layout_width="174dp"
        android:layout_height="41dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="50dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="150dp"
        android:onClick="goToAnotherActivity2"
        android:enabled="false"
        android:text="@string/scanning"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button4"
        android:layout_width="174dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="50dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="20dp"
        android:onClick="goToAnotherActivity3"
        android:enabled="false"
        android:text="@string/gaining_and_maintaining_access"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="180dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        android:onClick="goToAnotherActivity4"
        android:enabled="false"
        android:text="@string/report"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

This is the java code:这是 java 代码:

        package com.example.ProPentest;

        import android.content.Intent;
        import android.os.Bundle;
        import android.view.View;
        import android.widget.Button;

        import androidx.appcompat.app.AppCompatActivity;


        public class phase_list extends AppCompatActivity implements 
        View.OnClickListener {

        Button button2, button3, button4, button5;

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

            button2 = findViewById(R.id.button2);
            button3 = findViewById(R.id.button3);
            button4 = findViewById(R.id.button4);
            button5 = findViewById(R.id.button5);

            button2.setOnClickListener(this);

        }

        public void goToAnotherActivity1(View view) {
            Intent intent = new Intent(this, web_net.class);
            startActivity(intent);
        }

        public void goToAnotherActivity2(View view) {
            Intent intent = new Intent(this, web_net2.class);
            startActivity(intent);
        }

        public void goToAnotherActivity3(View view) {
            Intent intent = new Intent(this, webnet3.class);
            startActivity(intent);
        }

        public void goToAnotherActivity4(View view) {
            Intent intent = new Intent(this, report.class);
            startActivity(intent);
        }


        @Override
        public void onClick(View v) {
            if (v==button2){
                Intent myIntent = new Intent(phase_list.this, web_net.class);
                phase_list.this.startActivity(myIntent);
                button3.setEnabled(true);
                button4.setEnabled(false);
                button5.setEnabled(false);
            }

          if (v==button3) {
              Intent myIntent = new Intent(phase_list.this, web_net2.class);
              phase_list.this.startActivity(myIntent);
              button4.setEnabled(true);
              button5.setEnabled(false);
          }

            if (v==button4){
                Intent myIntent = new Intent(phase_list.this, webnet3.class);
                phase_list.this.startActivity(myIntent);
                button5.setEnabled(true);
        }
    }
}

The result should be like a process which user need to click the button step by step to enable the next button.结果应该类似于用户需要逐步单击按钮以启用下一步按钮的过程。

you defined both android:onClick and setOnClickListener to do different task simultaneously.I am not sure you can do it.您同时定义了android:onClicksetOnClickListener来执行不同的任务。我不确定您是否可以做到。 I think you should manage it in a listener.我认为你应该在听众中管理它。

Just add setOnClickListener(this);只需添加setOnClickListener(this); for button3,4 and 5 after assigning the ids to the variables.在将 id 分配给变量后用于 button3,4 和 5。

Like:- `喜欢: -`

button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
button5.setOnClickListener(this);`

Suggestion:- for better coding use switch statements inside onClick(View v) method for batter handling the particular button click.建议:- 为了更好地编码,使用 onClick(View v) 方法中的 switch 语句来处理特定的按钮单击。

Remove this section删除此部分

@Override
public void onClick(View v) {
    if (v==button2){
        Intent myIntent = new Intent(phase_list.this, web_net.class);
        phase_list.this.startActivity(myIntent);
        button3.setEnabled(true);
        button4.setEnabled(false);
        button5.setEnabled(false);
    }

  if (v==button3) {
      Intent myIntent = new Intent(phase_list.this, web_net2.class);
      phase_list.this.startActivity(myIntent);
      button4.setEnabled(true);
      button5.setEnabled(false);
  }

    if (v==button4){
        Intent myIntent = new Intent(phase_list.this, webnet3.class);
        phase_list.this.startActivity(myIntent);
        button5.setEnabled(true);}
}

Update this section更新此部分

 public void goToAnotherActivity1(View view) {
        Intent intent = new Intent(this, web_net.class);
        startActivity(intent);
    }

    public void goToAnotherActivity2(View view) {
        button3.setEnabled(true);
        button4.setEnabled(false);
        button5.setEnabled(false);
        Intent intent = new Intent(this, web_net2.class);
        startActivity(intent);

    }

    public void goToAnotherActivity3(View view) {
        button4.setEnabled(true);
        button5.setEnabled(false);
        Intent intent = new Intent(this, webnet3.class);
        startActivity(intent);
    }

    public void goToAnotherActivity4(View view) {
        button5.setEnabled(true);
        Intent intent = new Intent(this, report.class);
        startActivity(intent);
    }

Also i have notices that you just have implemented click listener programmatically only on button 2 in onCreate method so only call will return to onClick() overridden method for button 2我还注意到您只是在 onCreate 方法中的按钮 2 上以编程方式实现了单击侦听器,因此只有调用将返回按钮 2 的 onClick() 覆盖方法

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

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

        Button button1 = findViewById(R.id.button1);
        Button button2 = findViewById(R.id.button2);
        Button button3 = findViewById(R.id.button3);
        Button button4 = findViewById(R.id.button4);
        Button button5 = findViewById(R.id.button5);

        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        button4.setOnClickListener(this);
        button5.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button1:

              // you can put your code hear. As per button SetEnabled and Intent
                Toast.makeText(this, "Button 1 clicked", Toast.LENGTH_SHORT).show();
                break;
            case R.id.button2:
                Toast.makeText(this, "Button 2 clicked", Toast.LENGTH_SHORT).show();
                break;
            case R.id.button3:
                Toast.makeText(this, "Button 3 clicked", Toast.LENGTH_SHORT).show();
                break;
            case R.id.button4:
                Toast.makeText(this, "Button 4 clicked", Toast.LENGTH_SHORT).show();
                break;
            case R.id.button5:
                Toast.makeText(this, "Button 5 clicked", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}

Note: Remove in your xml file Button property android:onClick because in your code you Override the onClick method and goToAnotherActivity1/2/3/4 both are same thing. Note: Remove in your xml file Button property android:onClick because in your code you Override the onClick method and goToAnotherActivity1/2/3/4 both are same thing.

I hope it'll help you...!我希望它会帮助你......!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM