我设法在我的代码中使用了一个 spinner 并且想通过那个 spinner 改变 MainActivity 文件中某个文本的 textColor,但是他位于另一个类文件 - Einstellungen。
是否可以从另一个 Activity 更改当前 Activity 中的 textColor?
这是我要更改文本颜色的 main_activity.xml:
<TextView
android:id="@+id/speedtext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="180dp"
android:gravity="center"
android:singleLine="true"
android:text="TEXT"
android:textColor="@android:color/white"
android:textSize="220sp" />
这是 Einstellungen Activity :
public class Einstellungen extends AppCompatActivity {
String[] names = {"Weiß", "Blau", "Rot"};
String[] des = {"Weiß", "Blau", "Rot"};
ArrayAdapter<String> adapter;
Spinner spinner;
TextView description;
public Button button;
public void init() {
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent toy = new Intent(Einstellungen.this, MainActivity.class);
startActivity(toy);
}
});
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_einstellungen);
spinner = (Spinner) findViewById(R.id.spinner);
description = (TextView) findViewById(R.id.text);
adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, names);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
switch (i) {
case 0:
description.setText("" + des[i]);
MainActivity.speed.setTextColor(Color.WHITE);
break;
case 1:
description.setText("" + des[i]);
MainActivity.speed.setTextColor(Color.BLUE);
break;
case 2:
description.setText("" + des[i]);
MainActivity.speed.setTextColor(Color.RED);
break;
}
}
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
init();
}
}
主要 Activity :
public class MainActivity extends AppCompatActivity {
LocationService myService;
static boolean status;
LocationManager locationManager;
static TextView dist, time, speed;
static long startTime, endTime;
ImageView image;
static ProgressDialog locate;
static int p = 0;
private ServiceConnection sc = new ServiceConnection() {
public void onServiceConnected(ComponentName name, IBinder service) {
LocationService.LocalBinder binder = (LocationService.LocalBinder) service;
myService = binder.getService();
status = true;
}
public void onServiceDisconnected(ComponentName name) {
status = false;
}
};
public Button button;
public void init() {
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent toy = new Intent(MainActivity.this, Einstellungen.class);
startActivity(toy);
}
});
}
void bindService() {
if (status == true)
return;
Intent i = new Intent(getApplicationContext(), LocationService.class);
bindService(i, sc, BIND_AUTO_CREATE);
status = true;
startTime = System.currentTimeMillis();
}
void unbindService() {
if (status == false)
return;
Intent i = new Intent(getApplicationContext(), LocationService.class);
unbindService(sc);
status = false;
}
protected void onResume() {
super.onResume();
}
protected void onStart() {
super.onStart();
}
protected void onDestroy() {
super.onDestroy();
if (status == true)
unbindService();
}
public void onBackPressed() {
if (status == false)
super.onBackPressed();
else
moveTaskToBack(true);
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
speed = (TextView) findViewById(R.id.speedtext);
image = (ImageView) findViewById(R.id.image);
start();
init();
}
public void start() {
checkGps();
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
return;
}
if (status == false)
bindService();
locate = new ProgressDialog(MainActivity.this);
locate.setIndeterminate(true);
locate.setCancelable(false);
locate.setMessage("Suche GPS-Signal");
locate.show();
}
void checkGps() {
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
showGPSDisabledAlertToUser();
}
}
private void showGPSDisabledAlertToUser() {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setMessage("Bitte GPS aktivieren")
.setCancelable(false)
.setPositiveButton("GPS aktivieren",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent callGPSSettingIntent = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(callGPSSettingIntent);
}
});
alertDialogBuilder.setNegativeButton("Abbrechen",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}
}
最佳答案
首先,请不要将View 的 存储到静态字段中,这会导致Memory leaks。 .变化:
static ProgressDialog locate;
static TextView dist, time, speed;
到
private ProgressDialog locate;
private TextView dist, time, speed;
然后,为了您的目的,您可以使用 SharedPreferences .让我们一步一步来吧。
将下一个字段添加到 Einstellungen:
public static final String SHARED_PREFERENCES = "SHARED_PREFS";
public static final String SELECTED_COLOR = "SELECTED_COLOR";
private SharedPreferences preferences;
在 onCreate() 方法中获取 SharedPreferences:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_einstellungen);
preferences = getSharedPreferences(SHARED_PREFERENCES, MODE_PRIVATE);
...
}
将选定的颜色放入 SharedPreferences:
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
switch (i) {
case 0:
description.setText(des[i]);
preferences.edit().putInt(SELECTED_COLOR, Color.WHITE).apply();
break;
case 1:
description.setText(des[i]);
preferences.edit().putInt(SELECTED_COLOR, Color.BLUE).apply();
break;
case 2:
description.setText(des[i]);
preferences.edit().putInt(SELECTED_COLOR, Color.RED).apply();
break;
}
}
在您的 MainActivity 中:
添加下一个字段:
private SharedPreferences preferences;
在 onCreate() 方法中,获取选定的颜色并将其设置为 TextView:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
speed = findViewById(R.id.speedtext);
image = findViewById(R.id.image);
preferences = getSharedPreferences(Einstellungen.SHARED_PREFERENCES, MODE_PRIVATE);
int color = preferences.getInt(Einstellungen.SELECTED_COLOR, Color.WHITE);
speed.setTextColor(color);
init();
}
更新:
如果要保存spinner的状态,也可以使用SharedPreferences:
向 Einstellungen 添加另一个常量:
public static final String SELECTED_COLOR_POSITION = "SELECTED_COLOR_POSITION";
在 onItemSelected() 方法的开头添加下一行,以保存所选项目的位置:
preferences.edit().putInt(SELECTED_COLOR_POSITION, i).apply();
在 spinner.setAdapter(adapter) 行之后,在 onCreate() 方法中恢复微调器的状态:
int position = preferences.getInt(SELECTED_COLOR_POSITION, 0);
spinner.setSelection(position);
关于java - 更改安卓 :textColor with a spinner,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48640873/
如何正确创建Rails迁移,以便将表更改为MySQL中的MyISAM?目前是InnoDB。运行原始执行语句会更改表,但它不会更新db/schema.rb,因此当在测试环境中重新创建表时,它会返回到InnoDB并且我的全文搜索失败。我如何着手更改/添加迁移,以便将现有表修改为MyISAM并更新schema.rb,以便我的数据库和相应的测试数据库得到相应更新? 最佳答案 我没有找到执行此操作的好方法。您可以像有人建议的那样更改您的schema.rb,然后运行:rakedb:schema:load,但是,这将覆盖您的数据。我的做法是(假设
我在我的Rails项目中使用Pow和powifygem。现在我尝试升级我的ruby版本(从1.9.3到2.0.0,我使用RVM)当我切换ruby版本、安装所有gem依赖项时,我通过运行railss并访问localhost:3000确保该应用程序正常运行以前,我通过使用pow访问http://my_app.dev来浏览我的应用程序。升级后,由于错误Bundler::RubyVersionMismatch:YourRubyversionis1.9.3,butyourGemfilespecified2.0.0,此url不起作用我尝试过的:重新创建pow应用程序重启pow服务器更新战俘
我尝试使用不同的ssh_options在同一阶段运行capistranov.3任务。我的production.rb说:set:stage,:productionset:user,'deploy'set:ssh_options,{user:'deploy'}通过此配置,capistrano与用户deploy连接,这对于其余的任务是正确的。但是我需要将它连接到服务器中配置良好的an_other_user以完成一项特定任务。然后我的食谱说:...taskswithoriginaluser...task:my_task_with_an_other_userdoset:user,'an_othe
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
假设我有一个FireNinja我的数据库中的对象,使用单表继承存储。后来才知道他真的是WaterNinja.将他更改为不同的子类的最干净的方法是什么?更好的是,我很想创建一个新的WaterNinja对象并替换旧的FireNinja在数据库中,保留ID。编辑我知道如何创建新的WaterNinja来self现有FireNinja的对象,我也知道我可以删除旧的并保存新的。我想做的是改变现有项目的类别。我是通过创建一个新对象并执行一些ActiveRecord魔法来替换行,还是通过对对象本身做一些疯狂的事情,或者甚至通过删除它并使用相同的ID重新插入来做到这一点,这是问题的一部分。
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我
什么是ruby的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht
这篇文章是继上一篇文章“Observability:从零开始创建Java微服务并监控它(一)”的续篇。在上一篇文章中,我们讲述了如何创建一个Javaweb应用,并使用Filebeat来收集应用所生成的日志。在今天的文章中,我来详述如何收集应用的指标,使用APM来监控应用并监督web服务的在线情况。源码可以在地址 https://github.com/liu-xiao-guo/java_observability 进行下载。摄入指标指标被视为可以随时更改的时间点值。当前请求的数量可以改变任何毫秒。你可能有1000个请求的峰值,然后一切都回到一个请求。这也意味着这些指标可能不准确,你还想提取最小/
HashMap中为什么引入红黑树,而不是AVL树呢1.概述开始学习这个知识点之前我们需要知道,在JDK1.8以及之前,针对HashMap有什么不同。JDK1.7的时候,HashMap的底层实现是数组+链表JDK1.8的时候,HashMap的底层实现是数组+链表+红黑树我们要思考一个问题,为什么要从链表转为红黑树呢。首先先让我们了解下链表有什么不好???2.链表上述的截图其实就是链表的结构,我们来看下链表的增删改查的时间复杂度增:因为链表不是线性结构,所以每次添加的时候,只需要移动一个节点,所以可以理解为复杂度是N(1)删:算法时间复杂度跟增保持一致查:既然是非线性结构,所以查询某一个节点的时候