android - 如何在 Android 中更新 TextView 的文本?

标签 android android-activity textview listener

我在 Android 中有一个 View ,它有一个存储 x 和 y 值的 onTouchEvent。在我的 Activity 中,我有两个 TextViews,我最初将它们设置为 0,但希望在每次单击 View 时更新它们。

谁能解释一下如何做到这一点?

编辑

我的 Activity 显示 3 个不同的 View ,其中一个 View 有一个 onTouchEvent,用于查看单击时的 x 和 y 位置。

public class MyActivity extend Activity {
    //LinearLayouts created to hold the Views


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //add buttons 
        for(Button b : myButtons) {
            buttonLayout.addView(b);
        }

        clickLayout.addView(clickView);    //ClickView is where I use the onTouchEvent and want to get the values from

        for(TextView tv : myTextViews) {
            //This is where I would like to set the text
            //It needs to update every time the ClickView's onTouchEvent happens
            textLayout.addView(tv);
        }
    }
}

如果需要我的 ClickView 源代码,请告诉我,我会把它放在那里。然而,它只是获得了 X 和 Y。

Dan S 提到的选项(传递 TextView )是一个选项,但我希望我的 Activity 可以通过某种方式获取 ClickView 的 onTouchEvent。

如果没有上述方法的替代方法,我将继续并将其标记为已解决,因为上述选项确实有效,只是使我使用它的方式稍微复杂一些。

最佳答案

在 onTouchEvent 中使用这个伪代码

textViewX.setText(String.valueOf(motionEvent.getX()));
textViewY.setText(String.valueOf(motionEvent.getY()));

关于android - 如何在 Android 中更新 TextView 的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8144574/

相关文章:

android - 如何处理 Activity android

android - 如何在android的 TextView 中对长文本进行文本换行

android - 如何在 Style 中使用应用程序命名空间

android - 生成缩放版本的 ImageView 可绘制对象

android - 使用多个系列的不同颜色条

安卓构造器

java - 在android sdk的搜索 View 中更改文本字段的背景

android - NoClassDefFoundError : com/google/common/base/Predicate when importing MonkeyRunner in Jython

android - 如何从 EditText 中删除所有默认填充?

javascript - android textview 中带有 <style> 标签和样式的 Html 字符串