如何从另一个类获取按钮的文本

时间:2015-07-18 08:31:27

标签: java android

我有一个项目的arraylist,每个项目包含Main1类中的几个按钮。假设我想从Main类中获取该按钮的文本。那我怎么能这样做呢?

public class Main1 extends Activity implements View.OnClickListener {

Button button1,button2,button3,button4,button5,button6,button7,button8;

TextView textView;
private String a,b;
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main1);
    button1=(Button) findViewById(R.id.btn1);
    button2=(Button) findViewById(R.id.btn2);
    button3=(Button) findViewById(R.id.btn3);
    button4=(Button) findViewById(R.id.btn4);
    button5=(Button) findViewById(R.id.btn5);
    button6=(Button) findViewById(R.id.btn6);
    button7=(Button) findViewById(R.id.btn7);
    button8=(Button) findViewById(R.id.btn8);
    textView=(TextView) findViewById(R.id.txtsua);



    textView.setOnClickListener(this);
}
public String getA() {
    return button1.getText().toString();
}

public String getB() {
    return button2.getText().toString();
}

@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.btn1:

            break;
        case R.id.txtsua:
           startActivity(new Intent(this,Setup.class));

    }
}

设置类=>它显示按钮的文本

 public class Setup extends Activity {

 Main1 main=new Main1();

String a1,b1;
EditText editText;
Spinner spinner;

@Override

protected void onCreate(Bundle savedInstanceState) {
    a1=main.getA();
    b1=main.getB();
    ArrayList<String> ar=new ArrayList<String>();
    ar.add(a1);
    ar.add(b1);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_setup);
    editText=(EditText) findViewById(R.id.ed1);
    spinner=(Spinner) findViewById(R.id.spinner1);
    ArrayAdapter<String> arr=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,ar);
    arr.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);
    spinner.setAdapter(arr);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_setup, menu);
    return true;
}

2 个答案:

答案 0 :(得分:0)

您可以在进行第二次活动时将文本按钮置于意图中。 并将其检索到Setup Activity中。

Intent intent = new Intent(this,Setup.class);
intent.putExtras("text1",button1.getText().toString());
intent.putExtras("text2",button2.getText().toString());
startActivity(intent);

并在Setup.class中

Intent intent = getIntent();
String text1 = intent.getStringExtra("text1");
String text2 = intent.getStringExtra("text2");

答案 1 :(得分:0)

在意图中使用SharedPreferencesputExtra("key", "value")

方法1

Main1活动中:

SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

SharedPreferences.Editor editor = sharedpreferences.edit();

editor.putString("button1Text", button1.getText().toString());
//do this for all buttons
editor.commit();

然后在Setup活动中,

SharedPreferences sharedPreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
String button1Text = sharedPreferences.getString("button1Text");
//do this for all buttons

方法2

在Main1活动中:

Intent intent = new Intent(Main1.this, Setup.class);
intent.putExtras("button1Text", button1.getText().toString());
//do this for all buttons
startActivity(intent);

在设置活动中:

Intent intent = getIntent();
String button1Text = intent.getStringExtra("button1Text");
//do this for required buttons

如果您使用SharedPreferences,您也可以访问任何其他活动中的数据。