【问题标题】:Button is not reacting to click按钮对点击没有反应
【发布时间】:2019-01-14 15:56:35
【问题描述】:

我创建了一个简单的活动,旨在将两条信息发送到另一个活动。但是,我的按钮没有注册任何点击事件。有谁知道为什么会这样?我希望通过将 onClick = "onClickWrite" 添加到 XML 文件中,它们将被链接,但单击时没有响应。如果有人可以帮助我,我将不胜感激。我已经尝试实现 onClickListener,但是,通过该实现,它会在我的 Toast 上引发错误。

活动

public class InitializeClass extends Activity {

    private Service service;
    private Button button;
    EditText infoOne; 
    EditText infoTwo; 

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.button_control);

        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(clicked);
        infoOne= (EditText) findViewById(R.id.editText);
        infoTwo= (EditText) findViewById(R.id.editText1);

    }

    private View.OnClickListener clicked = new View.OnClickListener(){
        @Override
        public void onClick(View view) {
            if (service != null) {

                int size = 100;
                byte[] byteArray = new byte[size];
                byte[] byteArrayTwo = new byte[size];
                byteArray = infoOne.getText().toString().getBytes(Charset.defaultCharset());
                byteArrayTwo= infoTwo.getText().toString().getBytes(Charset.defaultCharset());

                if ((!(infoOne.getText().toString().isEmpty())) && (!(infoTwo.getText().toString().isEmpty()))) {
                    service.setInfo(byteArray);
                    service.setInfoTwo(byteArrayTwo);
                    intentMethod();
                }
            }
        }
    };

    public void intentMethod() {
        Intent intent = new Intent(this, DeviceScanActivity.class);
        startActivity(intent);
    }
}

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=".InitializeClass">


    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:text="@string/send_info"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        android:textColor="@color/white"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText1" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="150dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        android:textColorHint="@android:color/white"
        android:textCursorDrawable="@drawable/color_cursor"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:ems="10"
        android:hint="info"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        android:textColorHint="@android:color/white"
        android:textCursorDrawable="@drawable/color_cursor"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText" />

</android.support.constraint.ConstraintLayout>

【问题讨论】:

  • 看看this
  • 您的 java 类名与您的 xml 文件不匹配
  • @kartarkat 抱歉修复了这个问题。我最初改为类名是为了关注按钮问题。
  • @deHaar 如果我没看错,那么我的实现应该是正确的,因为我在我的 XML 文件中实现了 onClick 处理程序。
  • @LostandConfused 我想是的,您是否有任何错误消息或警告(请参阅 LogCat)?您是否尝试过仅在 Java 中实现它?这行得通吗?

标签: java android xml button onclick


【解决方案1】:

你需要先初始化你的按钮。

Button button = (Button) findViewById(R.id.button);

希望这会有所帮助!

【讨论】:

  • 遗憾的是我没有运气。我更新了我的代码,以便我在 Java 中执行此操作。您可能会看到任何其他想法或错误?
【解决方案2】:

在您的活动类中,将按钮分配给 xml 中按钮的 id:

private Button btn = findViewById(R.id.button);

然后您将创建一个 OnClickListener:

    private View.OnClickListener clicked = new View.OnClickListener(){
    @Override
    public void onClick(View view) {
        //Insert Code here
    }
};

在同一个活动类中的 onCreate 方法中,您将实例化按钮并将按钮分配给 onClickListener:

btn = new Button(this);
btn.setOnClickListener(clicked);

【讨论】:

  • 这也可以使用 XML 来完成,但是我发现最好只使用 XML 来处理 UI,而不是功能。
  • 遗憾的是我没有运气。我更新了我的代码,以便我在 Java 中执行此操作。您可能会看到其他任何想法或错误吗?
【解决方案3】:

为此,您需要做几件事。

  1. 创建字段。

    私有Button按钮;

  2. 初始化按钮。

    Button button = (Button) findViewById(R.id.button);

  3. 在按钮上设置监听器。

    button.setOnClickListener();

【讨论】:

  • 遗憾的是我没有运气。我更新了我的代码,以便我在 Java 中执行此操作。您可能会看到任何其他想法或错误?
【解决方案4】:

我找到了答案。 if (service != null) 中的服务从未被初始化。我犯了愚蠢的错误。谢谢大家的帮助并指导我实现onClickListener!

【讨论】:

    【解决方案5】:

    您需要使用 Java 代码转换按钮(名称)。 例如在 xml 你有 id/button (name) 在您的 Java 文件中声明它,然后进行强制转换。 onClickListener 看起来像这样:

    投下按钮:按钮按钮;

    Button button = (Button) findViewById(R.id.button);
    
        button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
    
                }
            });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-02
      • 2019-06-18
      • 2018-04-13
      • 2016-11-28
      • 1970-01-01
      • 2023-03-19
      • 2017-01-10
      • 2015-12-06
      相关资源
      最近更新 更多