簡體   English   中英

Android onClickListener和Button僅觸發一次

[英]Android onClickListener and Button only fires once

更新:解決了,問題出在我的activity_main.xml中,我在下面更新了我的代碼以反映我所做的更改。

我將再發表一篇文章,以正確的方式更新.java文件,以處理沒有ScrollView的文本滾動,由deathember描述。


更新:我改變了注釋this.terminal.append("Button# clicked...\\n"); 並添加this.terminal.setText("Button# clicked..."); 表現正常,每按一次按鈕都會更改文本,沒有問題。

有了這個,我知道我的buttonClicked方法正在被調用,但是文本沒有被附加到TextView上,如何解決這個問題有什么建議嗎?


我檢查了其他遇到此問題的帖子,但所有解決方案似乎都針對他們的應用程序。

我在ScrollView內有一個TextView,以提供類似於終端的行為,帶有4個按鈕,當單擊這些按鈕時,將文本追加到TextView上表示它們已被單擊。

但是,我只獲得一次點擊事件。 我可以單擊任何按鈕,它顯示單擊了正確的按鈕,但應用程序未注意到任何后續單擊。


我的Android活動主.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活動主.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>

我不確定為什么會這樣。 任何建議表示贊賞。

這是為您解決問題的代碼:)

 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="" />

並立即檢查代碼。 如有任何意見,請回覆。

要滾動TextView不一定位於ScrollView內TextView可以自行滾動。 需要更改TextView的布局,添加屬性

android: scrollbars = "vertical" 

活動並更改代碼,設置滾動方法

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>

結果:

結果:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM