2013-05-11 18 views
5

Tôi muốn đặt Văn bản thành TextView của ứng dụng của tôi (cam kết). Đây là mã tôi đã sử dụng:Tập hợp văn bản Android TextView phủ lên văn bản trước

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

Thực tế nó hoạt động, nhưng Văn bản trước đó vẫn được hiển thị! Ví dụ: 4 lớp phủ 8 (bạn không thể đọc được văn bản nữa).

Tôi có Hoạt động bắt đầu dịch vụ. Sau đó đăng ký LocalBroadcastReceiver cho Hoạt động. Các dịch vụ peridically gửi các đối tượng vị trí cho hoạt động. Từ vị trí tôi nhận được, ví dụ: kinh độ và muốn cập nhật một textview (chỉ chứa kinh độ) với kinh độ mới.

Tôi sẽ rất vui nếu có ai đó có ý tưởng giải quyết vấn đề này.

UPDATE: Mã đầy đủ khá dài, do đó đây là những mảnh vỡ có liên quan:

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

     ... 

} 
... 

Đây .xml bố trí của tôi:

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

Hy vọng điều đó sẽ hữu ích!

+2

bạn có thể đăng cho bạn mã đầy đủ & xml? R –

+0

đã cập nhật câu hỏi của tôi với mã không. – zuluk

Trả lời

4

Bạn sẽ không bao giờ tin điều này, nhưng nếu bạn đi qua số How often can you update a textview without mess, bạn sẽ tìm thấy câu hỏi và câu trả lời mà tôi nghĩ đang giải quyết vấn đề.

Nếu những điều này không giải quyết được sự cố, bạn có thể cần phải thay thế nút bằng Chế độ xem tùy chỉnh có ghi đè onDraw bằng cách sử dụng canvas.drawText() để đặt văn bản tại chỗ. Sau đó bạn có thể chắc chắn để lau bất kỳ lịch sử trước ...

NẾU bạn chỉ muốn một nền đen không minh bạch, tạo (hoặc thêm vào) file colors.xml trong thư mục res của bạn:

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

và thêm:

android:background="@color/black" 

Đến xml cho TextView.

+0

Tuyệt vời, đó là cùng một vấn đề. Cảm ơn nhiều! – zuluk

+0

Điều đó rất lạ! không nghe về nó! Có một cách khác để kiểm tra vấn đề này. hãy thử xóa tất cả các chế độ xem văn bản khác bên trong bố cục tuyến tính. có lẽ một trong số họ đang che phủ mặt khác. –

+0

@UdiOshi Đó là luôn luôn đáng kiểm tra, nhưng không phải là trường hợp trong câu hỏi tôi chỉ đến ... –

-1

Dòng android:text="" trong

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

là không cần thiết, bạn có thể xóa dòng đó.

+1

Đây không phải là câu trả lời cho câu hỏi ban đầu. Nó sẽ là tốt hơn như là một bình luận thay thế. –