Java 如何从另一个类获取按钮的文本
我有一个项目的arraylist,每个项目都包含Main1类中的几个按钮。假设我想从主类中获取该按钮的文本。那我该怎么做呢Java 如何从另一个类获取按钮的文本,java,android,Java,Android,我有一个项目的arraylist,每个项目都包含Main1类中的几个按钮。假设我想从主类中获取该按钮的文本。那我该怎么做呢 public class Main1 extends Activity implements View.OnClickListener { Button button1,button2,button3,button4,button5,button6,button7,button8; TextView textView; private String a,b; @Over
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));
}
}
设置类=>it place显示按钮的文本
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;
}
当您要进行第二个活动时,可以将文本按钮置于意图中。 并将其检索到安装活动中
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");
在意图中使用SharedReferences或PutextKey值
方法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();
然后在安装活动中
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
在安装活动中:
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
如果您使用SharedReferences,您也可以在任何其他活动中访问数据。请正确设置代码格式,如果我使用ar.adda…=>它工作正常,您现在发布的代码也最不可读。我不明白,请格式化你的代码,不仅仅是格式化,还要添加你想知道的内容。我们不能只看你的全部代码,而期望知道你具体在寻找什么。你不应该用新代码创建活动。看起来你对Android开发是如何完成的几乎一无所知,所以我建议在编写任何代码之前先阅读一些入门材料。我尝试使用方法2,但它不起作用,我不理解去掉Main1 main=new Main1;您不需要main.getA方法。在setContrentView之后,写入这些行Intent=getIntent;String button1Text=intent.getStringExtrabutton1Text//对所需的按钮执行此操作,然后创建ArrayList并将这些字符串添加到ArrayList。在使用“我的代码”后,是否可以显示安装活动的onCreate方法。