使用微调器

时间:2018-02-06 10:33:41

标签: java android android-studio spinner

我设法在我的代码中使用了spinner,并希望通过该微调器更改MainActivity文件中某个文本的textColor,但是它位于另一个类文件中 - {{1 }}。 是否可以从另一个活动中更改当前活动中的textColor?

这是我要更改文字颜色的Einstellungen

main_activity.xml


这是Einstellungen活动:

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


MainActivity:

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

1 个答案:

答案 0 :(得分:2)

首先,请永远不要将View's存储到静态字段中,它会导致Memory leaks。变化:

static ProgressDialog locate;
static TextView dist, time, speed;

private ProgressDialog locate;
private TextView dist, time, speed;

然后,出于您的目的,您可以使用SharedPreferences。让我们一步一步来。

  1. 将下一个字段添加到Einstellungen

    public static final String SHARED_PREFERENCES = "SHARED_PREFS";
    public static final String SELECTED_COLOR = "SELECTED_COLOR";
    private SharedPreferences preferences;
    
  2. SharedPreferences方法中获取onCreate()

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_einstellungen);
        preferences = getSharedPreferences(SHARED_PREFERENCES, MODE_PRIVATE);
        ...
    }
    
  3. 将所选颜色添加到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;
    
        }
    }
    
  4. MainActivity

    1. 添加下一个字段:

      private SharedPreferences preferences;
      
    2. 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();
      }
      
    3. <强>更新
      如果要保存微调器状态,还可以使用SharedPreferences

      1. 向Einstellungen添加另一个常数:

        public static final String SELECTED_COLOR_POSITION = "SELECTED_COLOR_POSITION";
        
      2. onItemSelected()方法的开头添加下一行,以保存所选的项目位置:

        preferences.edit().putInt(SELECTED_COLOR_POSITION, i).apply();
        
      3. onCreate()行之后spinner.setAdapter(adapter)方法中恢复微调器的状态:

        int position = preferences.getInt(SELECTED_COLOR_POSITION, 0);
        spinner.setSelection(position);