second.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <GridView android:id="@+id/GridView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:numColumns="auto_fit" android:stretchMode="columnWidth" android:gravity="center"> </GridView> </LinearLayout>
MyProject001Activity.java
package com.lin.test; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; public class MyProject001Activity extends Activity { /** Called when the activity is first created. */ EditText tv; public static int size=0; //定义静态变量,作为设定的每一行的单元格数。 //其实可以通过界面跳转intent传值,这里我简化了 Button button; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub final int bianchang= Integer.parseInt(tv.getText().toString()); size=bianchang*bianchang; startActivity(new Intent(MyProject001Activity.this,SecondActivity.class)); } }); } private void init() { // TODO Auto-generated method stub tv= (EditText) findViewById(R.id.tv); button= (Button) findViewById(R.id.button); } } main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/tv" android:hint="正方形的边长?" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/button" android:text="提交" /> </LinearLayout>
item.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView" android:layout_gravity="center" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv" android:layout_gravity="center" /> </LinearLayout>
效果图:
在界面A,输入5,点击提交
在界面B保证长行列的单元格数相同
最后别忘了清单文件
<activity android:name=".SecondActivity"/>