更新:解决了,问题出在我的activity_main.xml中 我已经更新了下面的代码,以反映我所做的更改。
我将再发布一个帖子来更新我的.java文件,使用正确的方法来处理文本滚动,而不是由deathember描述的ScrollView。
更新:
我更改了注释掉的行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>
我不确定为什么会这样。任何建议表示赞赏。
答案 0 :(得分:2)
以下是为您提供技巧的代码:)
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="" />
现在检查代码。如有任何问题请回复。
答案 1 :(得分:1)
要滚动 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>