【问题标题】:Android onClickListener and Button only fires onceAndroid onClickListener 和 Button 只触发一次
【发布时间】:2014-06-08 18:50:42
【问题描述】:

更新:已解决,问题出在我的 activity_main.xml 我已经更新了下面的代码以反映我所做的更改。

我将再发一篇文章更新我的 .java 文件,使用正确的方法来处理没有 ScrollView 的文本滚动,如 deadember 所述。


更新: 我更改了注释行 this.terminal.append("Button# clicked...\n"); 并添加了 this.terminal.setText("Button# clicked..."); ,其行为符合预期,并在每次按下按钮时更改文本,没有问题。

有了这个我知道我的 buttonClicked 方法被调用了,但是文本没有被附加到 TextView 中,有什么建议可以解决这个问题吗?


我查看了其他遇到此问题的帖子,但所有解决方案似乎都是针对他们的应用程序的。

我在 ScrollView 中有一个 TextView,以提供类似于终端的行为,带有 4 个按钮,当单击时将文本附加到 TextView,表示它们已被单击。

但是,我只收到一次点击事件。我可以点击任何按钮,它显示点击了正确的按钮,但应用程序不会注意到任何后续点击。


我的 Android Activity Main .java

package com.drj.pokemonar;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;

public class MainActivity extends Activity {

    TextView        terminal;
    ScrollView      terminalSView;

    Button          button1;
    Button          button2;
    Button          button3;
    Button          button4;

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

        this.terminal       = (TextView) findViewById(R.id.outputTerminal);
        this.terminalSView  = (ScrollView) findViewById(R.id.outputScrollView);

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

        // Necessary to bring the view to the bottom when
        //  a new line is appended.
        this.terminalSView.post(new Runnable() {
            @Override
            public void run() {
                ScrollView sView = (ScrollView) findViewById(R.id.outputScrollView);
                sView.fullScroll(View.FOCUS_DOWN);
            }
        });

    }

    // Defines the buttons behavior when clicked.
    public void buttonClicked(View v)
    {
        switch (v.getId())
        {
        case R.id.button1:
        {
            this.terminal.append("Button1 clicked...\n");
            break;
        }
        case R.id.button2:
        {
            this.terminal.append("Button2 clicked...\n");
            break;
        }
        case R.id.button3:
        {
            this.terminal.append("Button3 clicked...\n");
            break;
        }
        case R.id.button4:
        {
            this.terminal.append("Button4 clicked...\n");
            break;
        }
        default:
        {
            break;
        }
        }
    }
}

我的 Android Activity Main .xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${packageName}.${activityClass}" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:orientation="vertical" >

        <ScrollView
            android:id="@+id/outputScrollView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1" >

            <TextView
                android:id="@+id/outputTerminal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="" />

        </ScrollView>

        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TableRow
                android:id="@+id/tableRow1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <Button
                    android:id="@+id/button1"
                    style="?android:attr/buttonStyleSmall"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Button"
                    android:onClick="buttonClicked" />

                <Button
                    android:id="@+id/button2"
                    style="?android:attr/buttonStyleSmall"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"                    
                    android:text="Button"
                    android:onClick="buttonClicked" />

            </TableRow>

            <TableRow
                android:id="@+id/tableRow2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <Button
                    android:id="@+id/button3"
                    style="?android:attr/buttonStyleSmall"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Button"
                    android:onClick="buttonClicked" />

                <Button
                    android:id="@+id/button4"
                    style="?android:attr/buttonStyleSmall"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Button"
                    android:onClick="buttonClicked" />

            </TableRow>
        </TableLayout>

    </LinearLayout>

</RelativeLayout>

我不确定为什么会这样。任何建议表示赞赏。

【问题讨论】:

  • 如果先点击按钮1再点击按钮2,是否都添加到终端了?
  • 设置焦点向下滚动可能会将焦点从按钮拉到滚动视图本身?尝试删除您的可运行部分,然后看看会发生什么。 (只是为了测试)。
  • @MaxMeijer 不,不会添加后续按钮。如果我单击button2,它会正确显示它已被单击。然后点击button1,什么都没有。无论单击哪个按钮,都会发生同样的事情。只有第一次点击由监听器处理。
  • @zgc7009 我会尝试一下然后回复你。编辑:我注释掉那段代码没有影响。行为保持不变,只是它不会向下滚动。
  • 更新,每次按下按钮都会调用 buttonClicked 方法,但是 TextView 不会附加文本。我通过在 TextView 上调用 setText 而不是 append 对此进行了测试,它按预期工作。不知道从这里做什么。

标签: java android onclick onclicklistener android-button


【解决方案1】:

这是为您解决问题的代码:)

 public class MainActivity extends Activity {
    TextView terminal;
    Button button1, button2, button3, button4;

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

        terminal = (TextView) findViewById(R.id.outputTerminal);
        button1 = (Button) findViewById(R.id.button1);
        button2 = (Button) findViewById(R.id.button2);
        button3 = (Button) findViewById(R.id.button3);
        button4 = (Button) findViewById(R.id.button4);
    }

    // Defines the buttons behavior when clicked.
    public void buttonClicked(View v) {
        switch (v.getId()) {
        case R.id.button1:

            terminal.append("Button1 clicked...\n");
            break;

        case R.id.button2:

            terminal.append("Button2 clicked...\n");
            break;

        case R.id.button3:

            terminal.append("Button3 clicked...\n");
            break;

        case R.id.button4:

            terminal.append("Button4 clicked...\n");
            break;
        }
    }
}

我现在知道它可能会惹恼你。请将布局中的 TextView 更改为如下所示:

<TextView
                android:id="@+id/outputTerminal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="" />

现在检查代码。如果有任何问题,请评论。

【讨论】:

  • 抱歉,没解决。它唯一做的就是阻止它自动滚动。如果我遗漏了什么,除了从 onCreate 方法中删除对 terminalSView 的所有使用并从 switch 语句中删除默认情况之外,您是否还做了其他任何事情?
  • 我也用过 terminal.append();并删除了不需要的 run() ......我要求您将我的整个代码复制并粘贴到您的 activity_main.java 文件中,看看它是如何工作的。我在我身边对其进行了测试,它运行良好。
  • 我复制粘贴了你的代码,编译正常,行为没有改变,没有解决问题。问题可能是由我的模拟器或 android 库引起的吗?
  • 看起来你正在搞乱布局文件中的 TextView。我已经把修改后的TextView。请在您的 XML 文件中使用它并检查最终运行。一旦成功,我可以澄清是否有任何疑问......
  • 酷。您不必用不必要的 RunonUIThread 使您的代码复杂化......等等......除非它是必须的。因为它是一个简单的用例 :),所以代码对您来说很简单。欢迎来到 Android 编程......
【解决方案2】:

TextView的滚动不一定要装在ScrollView里面,TextView可以自己滚动。需要改变TextView的布局,添加属性

android: scrollbars = "vertical" 

activity及更改代码,设置滚动方式

import android.text.method.ScrollingMovementMethod; 

... 

mTexView.setMovementMethod (new ScrollingMovementMethod ()); 

ScrollView - 删除。

基于 Android SDK 的 Skeleton App 的示例。而是添加 EditText TextView:

<TextView android: id = "@ + id / editor" 
     android: layout_width = "match_parent" 
     android: layout_height = "0dip" 
     android: autoText = "true" 
     android: capitalize = "sentences" 
     android: layout_weight = "2" 
     android: freezesText = "true" 
     android: textSize = "120dip" 
     android: scrollbars = "vertical"> 
</ TextView>

工作代码:

import android.app.Activity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;

public class MainActivity extends Activity {

    TextView        terminal;
    ScrollView      terminalSView;

    Button          button1;
    Button          button2;
    Button          button3;
    Button          button4;

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

        this.terminal       = (TextView) findViewById(R.id.outputTerminal);
        terminal.setMovementMethod(new ScrollingMovementMethod());

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

    }

    // Defines the buttons behavior when clicked.
    public void buttonClicked(View v)
    {
        switch (v.getId())
        {
            case R.id.button1:
            {
                this.terminal.append("Button1 clicked...\n");
                break;
            }
            case R.id.button2:
            {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        terminal.append("Button2 clicked...\n");
                    }
                });
                break;
            }
            case R.id.button3:
            {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        terminal.append("Button3 clicked...\n");
                    }
                });

                break;
            }
            case R.id.button4:
            {
                this.terminal.append("Button4 clicked...\n");
                break;
            }
            default:
            {
                break;
            }
        }
    }
}

布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context="${packageName}.${activityClass}" >

    <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:orientation="vertical" >

            <TextView android:id="@+id/outputTerminal"
                      android:layout_width="match_parent"
                      android:layout_height="match_parent"
                      android:autoText="true"
                      android:capitalize="sentences"
                      android:layout_weight="2"
                      android:freezesText="true"
                      android:textSize="12sp"
                      android:scrollbars = "vertical" >
            </TextView>
        <TableLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

            <TableRow
                    android:id="@+id/tableRow1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" >

                <Button
                        android:id="@+id/button1"
                        style="?android:attr/buttonStyleSmall"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="Button"
                        android:onClick="buttonClicked" />

                <Button
                        android:id="@+id/button2"
                        style="?android:attr/buttonStyleSmall"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="Button"
                        android:onClick="buttonClicked" />

            </TableRow>

            <TableRow
                    android:id="@+id/tableRow2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" >

                <Button
                        android:id="@+id/button3"
                        style="?android:attr/buttonStyleSmall"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="Button"
                        android:onClick="buttonClicked" />

                <Button
                        android:id="@+id/button4"
                        style="?android:attr/buttonStyleSmall"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="Button"
                        android:onClick="buttonClicked" />

            </TableRow>
        </TableLayout>

    </LinearLayout>

</RelativeLayout>

结果:

【讨论】:

  • ScrollView 不是导致问题的原因。 TextView 根本没有附加文本,这让我难以置信。在 Ramakishna Balla 的回答中查看我的 cmets
  • @0x0byte 你试过我的例子了吗?
  • 我试试看。你能解释一下 runOnUIThread 方法的使用吗?为什么只有 2 个按钮包含它们?
  • @0x0byte 我就是这样做的。我的第一个想法 - 你从单独的后台线程中设置了一个值,但这个想法是错误的。
猜你喜欢
  • 2015-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-23
  • 2021-03-22
  • 2015-09-07
  • 2016-11-10
  • 2016-01-19
相关资源
最近更新 更多