安卓 TextView setText 覆盖之前的文本

5

我想将一个文本设置到我的应用程序的TextView中。这是我使用的代码:

TextView lonTextView = (TextView) findViewById(R.id.textViewLonWert);
lonTextView.setText(longitude.toString());

实际上它能运行,但之前的文本仍然显示!例如4覆盖8(您无法再读取文本)。
我有一个活动开始一个服务。然后向该活动注册一个本地广播接收器。该服务定期向活动发送位置对象。从位置中获取经度,并希望使用新经度更新textview(只包含经度)。
如果有人有解决此问题的想法,我将非常高兴。
更新: 完整代码非常长,因此这里是相关片段:
TrackingActivity.java
public class TrackingActivity extends Activity {

// GPSTracker class
GPSTracker gps; 

public String filename;

/* Um eigene Position anzuzeigen */
ArrayList<OverlayItem> overlayItemArray;
protected MapView mapView;
protected MapController mapController;
public Boolean isPause;
public Boolean isTracking;
public MyLocationOverlay myLocationOverlay;
PathOverlay myPath;
List<GeoPoint> path;

public BroadcastReceiver screenReceiver;

/* Textviews setzen */
TextView lonTextView;
TextView latTextView;
TextView altTextView;
TextView accTextView;

// Initiating Menu XML file (menu.xml)
/*@Override
public boolean onCreateOptionsMenu(Menu menu)
{

    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.menu_tracking, menu);
    MenuItem mi_startTracking = menu.findItem(R.id.start_tracking);
    mi_startTracking.setVisible(true);
    return true;
}*/

/**
 * Event Handling for Individual menu item selected
 * Identify single menu item by it's id
 * */
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    ...
}    

@Override

public boolean onPrepareOptionsMenu(Menu menu) {
   ...
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tracking);

    isTracking  = false;
    isPause     = false;

    // create GPS-class object
    gps = new GPSTracker(TrackingActivity.this
                        ,null);

    myPath  = new PathOverlay(Color.RED, this);
    path    = new ArrayList<GeoPoint>();

   /* Map anzeigen */
    mapView = new MapView(this, 500);
    mapView.setTileSource(TileSourceFactory.MAPNIK);
    org.osmdroid.views.MapView.LayoutParams mapParams = new                   org.osmdroid.views.MapView.LayoutParams(
            org.osmdroid.views.MapView.LayoutParams.MATCH_PARENT,
            org.osmdroid.views.MapView.LayoutParams.MATCH_PARENT,
            null, 0, 0, 0);
    /*löst Fehler aus: mapView.setClickable(true);*/
    mapView.setBuiltInZoomControls(true);
    mapController = new MapController(mapView);
    mapView.getController().setZoom(15);


    //--- Create Overlay
    overlayItemArray = new ArrayList<OverlayItem>();

    DefaultResourceProxyImpl defaultResourceProxyImpl   = new DefaultResourceProxyImpl(this);
    MyItemizedIconOverlay myItemizedIconOverlay         = new MyItemizedIconOverlay(
                                                        overlayItemArray, null, defaultResourceProxyImpl);
    mapView.getOverlays().add(myItemizedIconOverlay);
    //---

    //Add current position
    myLocationOverlay = new MyLocationOverlay(this, mapView);
    mapView.getOverlays().add(myLocationOverlay);

    /* Lege Pfad auf Karte */
    mapView.getOverlays().add(myPath);
    mapView.invalidate();
    LinearLayout myLayout = (LinearLayout) findViewById(R.id.trackingLinearLayoutMap);

    myLayout.addView(mapView,mapParams);

}

private BroadcastReceiver gpsDataReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {

        //Log.d("TrackingActivity", "in Receiver");
        /* alles was der Service liefert */
        String action       = intent.getAction();
        Double altitude     = intent.getDoubleExtra("altitude", 0);
        Double longitude    = intent.getDoubleExtra("longitude", 0);
        Double latitude     = intent.getDoubleExtra("latitude", 0);
        Bundle b            = intent.getExtras();
        Location location   = (Location)b.get("location");

        Log.d("received","Receiver: "+longitude+" ; "+latitude);


        //Log.d("TrackingActivity", "vor GeoPoint");
        GeoPoint locGeoPoint = new GeoPoint(latitude, longitude);

                /* Erstelle Pfad */
        if(isTracking){
            path.add(locGeoPoint);
            myPath.addPoint(locGeoPoint);
        }
        //Log.d("TrackingActivity", "nach GeoPoint: "+locGeoPoint.toString());
        mapController.setCenter(locGeoPoint);

        setOverlayLoc(location);

        /* Textviews Werte setzen */
        lonTextView = (TextView) findViewById(R.id.textViewLonWert);
        lonTextView.setText(longitude.toString());
        //lonTextView.invalidate();

        latTextView = (TextView) findViewById(R.id.textViewLatWert);
        latTextView.setText(latitude.toString());
        //latTextView.invalidate();

        altTextView = (TextView) findViewById(R.id.textViewAltWert);
        altTextView.setText(altitude.toString());
        //altTextView.invalidate();

        accTextView = (TextView) findViewById(R.id.textViewAccWert);
        accTextView.setText(Float.toString(location.getAccuracy())+" m");
        //accTextView.invalidate();

        /* Karte aktualisieren */
        mapView.invalidate();

    }
};
...
@Override
protected void onResume() {
    super.onResume();
    /* Registriere BroadcastReceiver für GPSDaten */
    LocalBroadcastManager.getInstance(this).registerReceiver(gpsDataReceiver, new IntentFilter("gpsdata"));

        ...

}
...

这是我的布局.xml文件:

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

<LinearLayout
    android:id="@+id/trackingStatusOuterWrapper"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="kein GPS Signal"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textSize="10sp" />

    <LinearLayout
    android:id="@+id/trackingStautsInnerWrapper2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
        <TextView
        android:id="@+id/textViewLonLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Länge: "
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textSize="10sp" />
        <TextView
        android:id="@+id/textViewLonWert"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textSize="10sp" />
        <TextView
        android:id="@+id/textViewLatLabel"
        android:layout_marginLeft="40dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Breite: "
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textSize="10sp" />
        <TextView
        android:id="@+id/textViewLatWert"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textSize="10sp" />

    </LinearLayout>
    <LinearLayout
    android:id="@+id/trackingStautsInnerWrapper3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

        <TextView
        android:id="@+id/textViewAltLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Höhe: "
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textSize="10sp" />
        <TextView
        android:id="@+id/textViewAltWert"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textSize="10sp" />
        <TextView
        android:id="@+id/textViewAccLabel"
        android:layout_marginLeft="40dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Genauigkeit: "
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textSize="10sp" />
        <TextView
        android:id="@+id/textViewAccWert"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textSize="10sp" />
    </LinearLayout>
</LinearLayout>
<!--  
<org.osmdroid.views.MapView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mapview"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:clickable="true"/>
-->

<LinearLayout
    android:id="@+id/trackingLinearLayoutMap"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

</LinearLayout>

希望这能有所帮助!


2
你能发布你的完整代码和XML吗? - Udi Oshi
我已经更新了我的问题,并加入了代码。 - zuluk
2个回答

4
您永远不会相信,如果您转到How often can you update a textview without mess,您将找到一个问题和答案,我认为这些是针对该问题的。

如果这些解决方案无法解决问题,您可能需要使用canvas.drawText()覆盖onDraw自定义视图来替换按钮以放置文本。然后,您可以确保清除任何以前的历史记录......

如果只想要一个不透明的黑色背景,请在res目录中创建(或添加到)colors.xml文件:

<resources>
    <color name="black">#ff000000</color>
</resources>

并添加:
    android:background="@color/black"

将 XML 应用于 TextView。


太棒了,这就是同样的问题。非常感谢! - zuluk
更新的答案以实现此目标。 - Neil Townsend
1
我仍然认为最好的方法是通过将其外部包含视图强制转换为ViewGroup来动态加载Widget。请参见我的答案:https://dev59.com/NX3aa4cB1Zd3GeqPeYio#22362483 - kandroidj
@inner_class7 不错的解决方案,因此点赞,下次遇到类似问题时我会比较不同的方法 :-) - Neil Townsend
@NeilTownsend 谢谢,是的,我对结果感到非常满意。这种方法可以让你拥有更多的控制权,并且你可以在任何小部件中使用它。只要注意所有子小部件都会继承父视图的布局参数即可。 - kandroidj
显示剩余7条评论

-2

在编程中,android:text="" 表示的是:

    <TextView
    android:id="@+id/textViewLonWert"
    android:layout_width="80dp"
    android:layout_height="wrap_content"
    android:text=""
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textSize="10sp" />

是多余的,你可以删除那一行。


1
这不是对原问题的答案。最好将其作为评论。 - Ben H

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接