用更新的 TextView 替换 TextView Android

Replacing a TextView with the updated TextView Android

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

我的 onActivityResult 方法:

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();
            }
        }
    }

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

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

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

可以通过调用Layout的indexOfChild方法获取view的index

但是,如果您仅通过更改 TextView 中的文本就可以实现您的尝试,则只需使用 TextView 的方法 setText()

所以基本上这归结为

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);

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

  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();
                }
            }
        }

您需要在 runOnUiThread 中更新您的 textview。

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

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

在您的代码中实现:

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 的更多实现:http://android.okhelp.cz/java_android_code.php?s=runOnUiThread