简体   繁体   English

单击一个按钮,TextView不会更新

[英]TextView wont update on click of a button

This, being my first attempt, may look naive. 这是我的第一次尝试,可能看起来很幼稚。 I have the activity_main.xml as below: 我有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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.buch.pranav.andy.hisabkitab.MainActivity" >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="30dp"
    android:onClick="clickHandler"
    android:text="@string/connect" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/button1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="65dp"
    android:text="@string/testVal2"
    android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

and my MainActivity.java would look like this: 和我的MainActivity.java看起来像这样:

public class MainActivity extends ActionBarActivity {

//Auto generated code here

public void clickHandler(View view){
    View header = (View)getLayoutInflater().inflate(R.layout.activity_main, null);
    TextView tv = (TextView) header.findViewById(R.id.textView2);
    System.out.println("tv = "+tv.getText());
    tv.setText("lol");
}

//Auto generated code here 
}

Now when I click the button, I see the sysout in logcat window so there are no issues with the binding of clickHandler, but the textview on my app screen will not update! 现在,当我单击按钮时,我在logcat窗口中看到sysout,因此clickHandler的绑定没有任何问题,但是我的应用程序屏幕上的textview不会更新! Logcat doesn't even show any exception being thrown. Logcat甚至不显示任何抛出的异常。

What am I missing here? 我在这里想念什么?

As far as I can see you use the wrong layout because you're inflating it inside of your click-handle instead of using the layout which you should have inflated in "onCreate" or "onCreateView". 据我所知,您使用了错误的布局,因为您是在单击手柄内部对其进行了膨胀,而不是使用应在“ onCreate”或“ onCreateView”中进行膨胀的布局。

Initialize your view inside of onCreate, keep a reference to the TextView and modify only this TextView from the reference. 在onCreate内初始化视图,保留对TextView的引用,并仅从引用中修改此TextView。

What you currently do is: Create a view - modify it - remove it (it's never attached to your window) 您当前要做的是:创建视图-对其进行修改-将其删除(该视图永远不会附加到您的窗口中)

Do something like this instead: 做这样的事情:

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

  final TextView textView2 = (TextView) findViewById(R.id.textView2);
  final Button button = (Button) findViewById(R.id.button1);
  button.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {
           textView2.setText("lol");
        }

    });
}

You are inflating the layout XML, but not attaching it anywhere in your activity's view hierarchy nor setting it as the content view of your activity. 您正在放大布局XML,但未将其附加到活动的视图层次结构中的任何位置,也未将其设置为活动的内容视图。 So you are updating the text in your TextView, but it is not actually visible on the screen. 因此,您正在更新TextView中的文本,但实际上在屏幕上不可见。

You will want to do something like: 您将需要执行以下操作:

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

Or, if you don't want the layout to be your main content view, attach it appropriately within your view hierarchy. 或者,如果您不希望布局成为主要内容视图,则将其适当地附加到视图层次结构中。

Try this, 尝试这个,

activity_main.xml 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.newproject.MainActivity" >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="30dp"
    android:onClick="clickHandler"
    android:text="@string/connect" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/button1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="65dp"
    android:text="@string/textView2"
    android:textAppearance="?android:attr/textAppearanceMedium" />

MainActivity.java MainActivity.java

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

    TextView tv;
    Button button;

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

    }

    public void clickHandler(View v) {
        tv = (TextView) findViewById(R.id.textView2);
        button = (Button) findViewById(R.id.button1);
        System.out.println("tv = " + tv.getText());
        tv.setText("lol");

    }
    }

strings.xml strings.xml

 <?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">NewProject</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string name="connect">Connect</string>
    <string name="textView2">This is the text</string>

</resources>

Output : 输出

When the app starts, 应用启动后,

应用运行时

After clicking connect button, 点击connect按钮后,

在此处输入图片说明

In Logcat ,you can see the text in green color which gets from TextView . Logcat ,您可以看到从TextView获得的绿色文本。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM