我想将文本设置到我的应用程序(许可)的 TextView 中。这是我使用的代码:
TextView lonTextView = (TextView) findViewById(R.id.textViewLonWert);
lonTextView.setText(longitude.toString());
居然能用,但是还是显示之前的Text!例如,4 覆盖了 8(您无法再阅读文本)。
我有一个启动服务的 Activity 。然后向 Activity 注册一个 LocalBroadcastReceiver。该服务定期将位置对象发送到 Activity 。从我得到的位置经度并想用新经度更新 TextView (仅包含经度)。
如果有人有解决这个问题的想法,我将非常高兴。
更新: 完整的代码很长,所以这里是相关的 fragment :
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>
希望对您有所帮助!
最佳答案
你永远不会相信这一点,但如果你前往 How often can you update a textview without mess您会找到我认为可以解决问题的问题和答案。
如果这些不能解决问题,您可能需要将按钮替换为自定义 View ,并使用 canvas.drawText() 覆盖 onDraw 以放置文本。然后您可以确保删除任何以前的历史记录......
如果您只想要一个非透明的黑色背景,请在您的 res 目录中创建(或添加到)colors.xml 文件:
<resources>
<color name="black">#ff000000</color>
</resources>
并添加:
android:background="@color/black"
到 TextView 的 xml。
关于Android TextView setText 覆盖之前的Text,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16498015/
我有一个包含模块的模型。我想在模块中覆盖模型的访问器方法。例如:classBlah这显然行不通。有什么想法可以实现吗? 最佳答案 您的代码看起来是正确的。我们正在毫无困难地使用这个确切的模式。如果我没记错的话,Rails使用#method_missing作为属性setter,因此您的模块将优先,阻止ActiveRecord的setter。如果您正在使用ActiveSupport::Concern(参见thisblogpost),那么您的实例方法需要进入一个特殊的模块:classBlah
所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择
我在pry中定义了一个函数:to_s,但我无法调用它。这个方法去哪里了,怎么调用?pry(main)>defto_spry(main)*'hello'pry(main)*endpry(main)>to_s=>"main"我的ruby版本是2.1.2看了一些答案和搜索后,我认为我得到了正确的答案:这个方法用在什么地方?在irb或pry中定义方法时,会转到Object.instance_methods[1]pry(main)>defto_s[1]pry(main)*'hello'[1]pry(main)*end=>:to_s[2]pry(main)>defhello[2]pry(main)
我有一个服务模型/表及其注册表。在表单中,我几乎拥有服务的所有字段,但我想在验证服务对象之前自动设置其中一些值。示例:--服务Controller#创建Action:defcreate@service=Service.new@service_form=ServiceFormObject.new(@service)@service_form.validate(params[:service_form_object])and@service_form.saverespond_with(@service_form,location:admin_services_path)end在验证@ser
在Ruby类中,我重写了三个方法,并且在每个方法中,我基本上做同样的事情:classExampleClassdefconfirmation_required?is_allowed&&superenddefpostpone_email_change?is_allowed&&superenddefreconfirmation_required?is_allowed&&superendend有更简洁的语法吗?如何缩短代码? 最佳答案 如何使用别名?classExampleClassdefconfirmation_required?is_a
我们的git存储库中目前有一个Gemfile。但是,有一个gem我只在我的环境中本地使用(我的团队不使用它)。为了使用它,我必须将它添加到我们的Gemfile中,但每次我checkout到我们的master/dev主分支时,由于与跟踪的gemfile冲突,我必须删除它。我想要的是类似Gemfile.local的东西,它将继承从Gemfile导入的gems,但也允许在那里导入新的gems以供使用只有我的机器。此文件将在.gitignore中被忽略。这可能吗? 最佳答案 设置BUNDLE_GEMFILE环境变量:BUNDLE_GEMFI
我正在我的Rails项目中安装Grape以构建RESTfulAPI。现在一些端点的操作需要身份验证,而另一些则不需要身份验证。例如,我有users端点,看起来像这样:moduleBackendmoduleV1classUsers现在如您所见,除了password/forget之外的所有操作都需要用户登录/验证。创建一个新的端点也没有意义,比如passwords并且只是删除password/forget从逻辑上讲,这个端点应该与用户资源。问题是Grapebefore过滤器没有像except,only这样的选项,我可以在其中说对某些操作应用过滤器。您通常如何干净利落地处理这种情况?
在我做的一些网络开发中,我有多个操作开始,比如对外部API的GET请求,我希望它们同时开始,因为一个不依赖另一个的结果。我希望事情能够在后台运行。我找到了concurrent-rubylibrary这似乎运作良好。通过将其混合到您创建的类中,该类的方法具有在后台线程上运行的异步版本。这导致我编写如下代码,其中FirstAsyncWorker和SecondAsyncWorker是我编写的类,我在其中混合了Concurrent::Async模块,并编写了一个名为“work”的方法来发送HTTP请求:defindexop1_result=FirstAsyncWorker.new.async.
-if!request.path_info.include?'A'%{:id=>'A'}"Text"-else"Text"“文本”写了两次。我怎样才能只写一次并同时检查path_info是否包含“A”? 最佳答案 有两种方法可以做到这一点。使用部分,或使用content_forblock:如果“文本”较长,或者是一个重要的子树,您可以将其提取到一个部分。这会使您的代码变干一点。在给出的示例中,这似乎有点矫枉过正。在这种情况下更好的方法是使用content_forblock,如下所示:-if!request.path_info.inc
假设您编写了一个类Sup,我决定将其扩展为SubSup。我不仅需要了解你发布的接口(interface),还需要了解你的私有(private)字段。见证这次失败:classSupdefinitialize@privateField="fromsup"enddefgetXreturn@privateFieldendendclassSub问题是,解决这个问题的正确方法是什么?看起来子类应该能够使用它想要的任何字段而不会弄乱父类(superclass)。编辑:equivalentexampleinJava返回"fromSup",这也是它应该产生的答案。 最佳答案