【发布时间】:2012-01-10 21:44:48
【问题描述】:
我有一个带有 5 个按钮的主屏幕。每次按一个按钮,我都想进入一个新屏幕。到目前为止,我还有其他 5 个类(每个用于每个屏幕)和 5 个 xml。但我相信会有更好的方法,因为这个屏幕和 xml 是相同的,我想要做的是更改一些文本和我从数据库中获取的一些数据。我确信我可以只使用另一个类和一个 xml,然后将我想要的值作为参数传递。 (想象一下,在最终状态下,我的应用程序必须有 15 个按钮,所以我认为太浪费空间了,没有必要拥有 15 个看起来相同的 .java 文件和 15 个 xml 文件,并且只有一些图像和文本视图的值发生变化) .我的主要活动代码是:
public class MyActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
main2Theme();}
private void main2Theme() {
setContentView(R.layout.main_new);
Button Button100 = (Button)findViewById(R.id.Button100);
Button113.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent(this, OtherScreenName.class);
startActivity(i);
}
}); //and so on for 15 buttons
我的 OtherScreenName.class 是:
public class OtherScreenName extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Theme();
}
@SuppressWarnings("null")
private void Theme() {
Log.d("SnowReportApp","Do first thing");
setContentView(R.layout.otherscreenname); //THIS CHANGES IN EVERY CLASS DEPENDING ON THE ID OF THE BUTTON
String result = "";
InputStream is = null;
StringBuilder sb=null;
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();//() before
nameValuePairs.add(new BasicNameValuePair("ski_id","OtherScreenName"));
TextView text1 = (TextView) findViewById(R.id.otherscreenname_1);
//THESE 2 CHANGE DEPERNDING ON THE BUTTON PRESSED
//perform action.....
//AND ALSO HERE I NEED THE ID OF THE BUTTON
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(OtherScreenName.this,MyActivity.class);
startActivity(i);
}
});
谁能建议如何为我的班级提供论据以及它们的类型应该是什么?
【问题讨论】: