【问题标题】:Android Changing Text of TextView on Button click not workingAndroid 在按钮单击时更改 TextView 的文本不起作用
【发布时间】:2012-06-03 22:51:45
【问题描述】:

如标题所示,我正在尝试在我的 Android 应用程序中创建一个按钮来更改 TextView,但它无法正常工作......这是代码,希望你们看看出了什么问题。仅供参考,我正在使用连接了 Android 2.3.3 模拟器的 NetBeans,因为稍后我将在手机 (2.3.6) 上运行。

主java:

public class Rooster extends Activity
{
    private Button buttonSearch;

    /** Called when the activity is first created. */
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        this.buttonSearch = (Button)this.findViewById(R.id.buttonSearch);
        this.buttonSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView textRooster = (TextView)findViewById(R.id.textRooster); 
                textRooster.setText("some text"); 
            }
        });
    }

这是我的 main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="42px"
        android:layout_weight="1"
        android:gravity="center_vertical" >

        <EditText
            android:id="@+id/editSearch"
            android:layout_width="0dip"
            android:layout_height="37px"
            android:layout_weight="2"
            android:gravity="center"
            android:hint="Zoekopdracht" />

        <Button
            android:id="@+id/buttonSearch"
            android:layout_width="0dip"
            android:layout_height="37px"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Zoek" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="39px"
        android:layout_weight="1"
        android:gravity="center" >

        <Button
            android:id="@+id/buttonChangeWijzigingen"
            android:layout_width="fill_parent"
            android:layout_height="37px"
            android:gravity="center"
            android:text="Zet wijzigingen aan" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="12" >

        <TextView
            android:id="@+id/textRooster"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:gravity="center_horizontal"
            android:text="Geen rooster opgezocht" />

    </LinearLayout>

</LinearLayout>

【问题讨论】:

  • “它不能正常工作”..究竟是什么方式?也发布您的 logcat 内容。
  • 你的代码没有问题
  • Kazekage Gaara,如果我点击按钮,它只是没有做任何事情! Logcat 稍后会发布。
  • 我只是测试你的代码,它可以工作。

标签: android textview android-button


【解决方案1】:

我认为您认为单击的按钮实际上已被覆盖。尝试改变

    this.buttonSearch = (Button)this.findViewById(R.id.buttonSearch);

    this.buttonSearch = (Button)this.findViewById(R.id.buttonChangeWijzigingen);

它实际上并没有解决问题,只是让问题更清楚。

【讨论】:

  • 嗨!首先,感谢您的回复。拉过来是什么意思?我改变了它,但这并没有改变这种情况。
  • 如果您没有正确设置 xml 布局,有时部分或整个组件可能会被覆盖(一个组件放在另一个组件之上)。当我第一次开始使用android时,我遇到了很多这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-21
  • 1970-01-01
  • 1970-01-01
  • 2012-01-29
  • 1970-01-01
相关资源
最近更新 更多