单击时如何使网格视图打开特定的新页面?

时间:2013-07-19 05:34:36

标签: java android android-layout

有人可以查看我的代码并告诉我我做错了什么。代码从数组中的位置检索ID,使用switch语句,并根据所选内容更改布局。有什么建议? (此处未列出,但代码已导入所有适当的类和包)

public class Newpage extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {


      // get intent data
    Intent i = getIntent();

    // Selected image id
    int position = i.getExtras().getInt("id");
 // based on position switches to its respective screen

    switch (position)
    {
   case 6:
       super.onCreate(savedInstanceState);
       TextView view = (TextView) findViewById(R.layout.page1);
       this.setContentView(view);
       break;
   case 0:
       super.onCreate(savedInstanceState);
       view = (TextView) findViewById(R.layout.page2);
       this.setContentView(view);
       break;
   case 1:
       super.onCreate(savedInstanceState);
       view = (TextView) findViewById(R.layout.page3);
       this.setContentView(view);
       break;
   case 2:
       super.onCreate(savedInstanceState);
       view = (TextView) findViewById(R.layout.page4);
       this.setContentView(view);
       break;
   case 3:
       super.onCreate(savedInstanceState);
       view = (TextView) findViewById(R.layout.page5);
       this.setContentView(view);
       break;
   case 4:
       super.onCreate(savedInstanceState);
       view = (TextView) findViewById(R.layout.page6);
       this.setContentView(view);
       break;
   case 7:
       super.onCreate(savedInstanceState);
       view = (TextView) findViewById(R.layout.page7);
       this.setContentView(view);
       break;
    }



}

}

2 个答案:

答案 0 :(得分:0)

您想要更改textview的活动或内容吗? textview是布局的一个组件。你必须使用setContentView(R.layout.yourlayout)设置布局的内容,如果你想改变textView的内容你首先声明文本视图TextView mytextView =(TextView)findViewById(R.id.textView1);然后你使用mytextView.setText(“这是一个字符串”);

答案 1 :(得分:0)

    TextView view = (TextView) findViewById(R.layout.page1);

你为什么这样做?