Android:使用其他活动更改文本视图

时间:2011-07-18 20:56:44

标签: android resources android-activity android-intent textview

我正在尝试更改textview的textview属性,如下所示:

textview properties

在单独的活动中,如下所示:

Activity

我尝试用捆绑包来做这件事,但我无法让它工作。 这就是我的BookActivity的样子:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.book_activity);

    //this is where the size property comes in
    Integer size = getIntent().getExtras().getInt("SGRkey");
    TextView test2 = (TextView) findViewById(R.id.booktext);
    test2.setTextSize(size);

    Spinner spinner = (Spinner) findViewById(R.id.kapitelspinner);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this, R.array.kapitel_array, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());


}
    public class MyOnItemSelectedListener implements OnItemSelectedListener {

        public void onItemSelected(AdapterView<?> parent,
            View view, int pos, long id) {
          Toast.makeText(parent.getContext(),  
              parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();

          final String[] theporn = getResources().getStringArray(R.array.allkapitel);



          TextView text = (TextView) findViewById(R.id.booktext);
          text.setText(theporn[pos]); 


        }

        public void onNothingSelected(AdapterView parent) {
          // Do nothing.
        }

(我在微调器中选择章节字符串,效果很好。)

这就是我的SettingsActivity的样子:

public class SettingsActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.settings_view);

    Spinner SGRspinner = (Spinner) findViewById(R.id.schriftgroeße_spinner);
    ArrayAdapter<CharSequence> SGRadapter = ArrayAdapter.createFromResource(
            this, R.array.schriftgroesse_list, android.R.layout.simple_spinner_item);
    SGRadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    SGRspinner.setAdapter(SGRadapter); 

}


public class SGROnItemSelectedListener implements OnItemSelectedListener {

    public void onItemSelected(AdapterView<?> parent,
        View view, int pos, long id) {

        Intent answer = new Intent();


      Toast.makeText(parent.getContext(),  
          parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();



      final String[] SGRstring = getResources().getStringArray(R.array.schriftgroesse_list);
      int SGRint = Integer.parseInt(SGRstring[pos]);

      Bundle size = new Bundle();
      size.putInt("SGRkey", SGRint);
      Intent nextActivity = new Intent(com.asm.reader.SettingsActivity.this, com.asm.reader.BookActivity.class);
      nextActivity.putExtras(size);

      com.asm.reader.SettingsActivity.this.startActivity(nextActivity);

    }

    public void onNothingSelected(AdapterView parent) {
      // Do nothing.
    }


}

我尝试这个时出错了。所有活动都在清单中声明。我真的不知道该怎么做。我对这个很新,很抱歉,如果这很简单,但任何帮助都会非常感激!! : - )

1 个答案:

答案 0 :(得分:5)

使您的textview保持静态。也就是说,将其声明为公共静态类变量。然后你可以直接从其他活动中调用它:firstActivity.myTextView.setText(“foo”);