首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用更新的TextView Android替换TextView

用更新的TextView Android替换TextView
EN

Stack Overflow用户
提问于 2015-05-12 04:47:28
回答 3查看 1.1K关注 0票数 2

我正在动态地在活动布局中添加一个TextView。该活动使用startActivityForResult从另一个活动获取值,然后动态创建textView并向其添加特定值。但问题是,当我再次获得值并返回到负责显示textView的活动时,同一个活动有一个update按钮,它没有更新该textView,而是在前面的textView下面添加了另一个textView,在布局中,它继续添加而不进行更新。我知道我们可以将它的可见性设置为gone,但是它是否也从布局的子元素中删除了textView,或者还有另一种方法来破坏文本视图并在那个地方创建另一个呢?

My onActivityResult方法:

代码语言:javascript
复制
protected void onActivityResult(int requestCode, int resultCode, Intent intentData) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, intentData);
        if(requestCode == REQUEST_CODE && resultCode == RESULT_OK){
            locationLat = intentData.getDoubleExtra("LocationLat", 0.0);
            locationLng = intentData.getDoubleExtra("LocationLang", 0.0);
            location = locationLat + " "+locationLng;
            if(location != ""){
                Toast.makeText(this, location, Toast.LENGTH_LONG).show();
                lblName = new TextView(this);
                lblName.setText(location);
                lblName.setId(9);
                lblName.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
                LinearLayout ll = (LinearLayout) findViewById(R.id.llOne);
                ll.addView(lblName, 5);
                Button btnSetLoc = (Button) findViewById(R.id.getLoc);
                btnSetLoc.setText("Change Location");
            }
            else{
                Toast.makeText(this, "location is not set", Toast.LENGTH_LONG).show();
            }
        }
    }
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-05-12 04:51:04

尝试检查lblName是否为空,然后创建它,否则更新它

代码语言:javascript
复制
  protected void onActivityResult(int requestCode, int resultCode, Intent intentData) {
            // TODO Auto-generated method stub
            super.onActivityResult(requestCode, resultCode, intentData);
            if(requestCode == REQUEST_CODE && resultCode == RESULT_OK){
                locationLat = intentData.getDoubleExtra("LocationLat", 0.0);
                locationLng = intentData.getDoubleExtra("LocationLang", 0.0);
                location = locationLat + " "+locationLng;
                if(location != ""){
                    Toast.makeText(this, location, Toast.LENGTH_LONG).show();
                    if(lblName==null) {
                        lblName = new TextView(this);
                        lblName.setId(9);
                        lblName.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                        LinearLayout ll = (LinearLayout) findViewById(R.id.llOne);
                        ll.addView(lblName, 5);
                        Button btnSetLoc = (Button) findViewById(R.id.getLoc);
                        btnSetLoc.setText("Change Location");
                    }
                    lblName.setText(location);
                }
                else{
                    Toast.makeText(this, "location is not set", Toast.LENGTH_LONG).show();
                }
            }
        }
票数 1
EN

Stack Overflow用户

发布于 2015-05-12 04:50:44

确实要删除文本视图而不更改其值吗?

更改TextView的文本比从布局中删除TextView并重新附加新的文本要便宜得多

但是,如果您想要删除TextView并附加一个新的,您可以首先使用布局的removeView方法删除旧的,然后使用布局的addView方法插入新的

可以通过调用布局的indexOfChild方法来获取视图的索引

但是,如果您可以通过更改TextView中的文本来实现您正在尝试的目的,那么只需使用TextView的方法setText()

所以从根本上讲,这是什么

代码语言:javascript
复制
TextView oldText = (TextView) findViewById(R.id.oldText);
LinearLayout ll = (LinearLayout) findViewById(R.id.llOne);
int index = ll.indexOfChild(oldText);
ll.removeViewAt(index);
TextView newText = new TextView(context);
ll.addView(newText, index);
票数 1
EN

Stack Overflow用户

发布于 2015-05-12 06:18:11

您需要更新runOnUiThread中的文本视图。

runOnUiThread:在UI线程上运行指定的操作。如果当前线程是UI线程,则立即执行该操作。如果当前线程不是UI线程,则该操作将被发布到UI线程的事件队列中。

参考http://developer.android.com/reference/android/app/Activity.html#runOnUiThread(java.lang.Runnable)

在您的代码中实现:

代码语言:javascript
复制
protected void onActivityResult(int requestCode, int resultCode, Intent intentData) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, intentData);
        if(requestCode == REQUEST_CODE && resultCode == RESULT_OK){
            locationLat = intentData.getDoubleExtra("LocationLat", 0.0);
            locationLng = intentData.getDoubleExtra("LocationLang", 0.0);
            location = locationLat + " "+locationLng;
            if(location != ""){
                 runOnUiThread(new  Runnable() { 
                public  void  run() { 
                   Toast.makeText(this, location, Toast.LENGTH_LONG).show();
                lblName = new TextView(this);
                lblName.setText(location);
                lblName.setId(9);
                lblName.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
                LinearLayout ll = (LinearLayout) findViewById(R.id.llOne);
                ll.addView(lblName, 5);
                Button btnSetLoc = (Button) findViewById(R.id.getLoc);
                btnSetLoc.setText("Change Location");
                } 
            }); 

            }
            else{
                Toast.makeText(this, "location is not set", Toast.LENGTH_LONG).show();
            }
        }
    }

有关runOnUiThread的更多实现:code.php?s=runOnUiThread

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30181977

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档