Android游戏开发学习(4)--界面的切换的两种方式
在写这两个例子的时间特别郁闷,开始以为EditText.getText()获得是字符串呢?就直接放进Intent中传给第二个Activity了,结果一直获得都是null。
还来终于发现了EditText.getText()返回的不是字符串,原来是Editable对象。
intent.putExtra("name", et.getText() );
intent.getStringExtra("name");//获得是为null
intent.getCharSequenceExtra("name")//可以正常获得所输入值
----------------------------------------------------------------------------------------------------------------------------------
intent.putExtra("name", et.getText().toString() );
intent.getStringExtra("name");/可以正常获得所输入值
intent.getCharSequenceExtra("name")//可以正常获得所输入值
----------------------------------------------------------------------------------------------------------------------------------
在android实现界面的切换有两种方式
1.就是两个Activity切换
首先创建两个Activity,然后在AndroidManifest.xml中注册,最后在一个Activity中调用另一个Activity
public class Activity01 extends Activity { EditText et; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main01); et=(EditText)findViewById(R.id.t1); } public void submit(View view){ System.out.println(et.getText()); Intent intent=new Intent(); intent.putExtra("name", et.getText().toString()); intent.setClass(Activity01.this, Activity02.class); Activity01.this.startActivity(intent); } public void submitByBundle(View view){ Intent intent=new Intent(); //使ç¨Bundle Bundle b=new Bundle(); b.putString("name", et.getText().toString()); intent.putExtras(b); intent.setClass(this, Activity02.class); startActivity(intent); } }
public class Activity02 extends Activity { TextView tv; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main02); Intent intent=getIntent(); tv=(TextView)findViewById(R.id.t2); tv.setText("ä½ è¾å ¥çä¿¡æ¯æ¯ï¼"+intent.getStringExtra("name")); } }
<?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" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="è¾å ¥ä¿¡æ¯ï¼" /> <EditText android:id="@+id/t1" android:layout_width="match_parent" android:layout_height="wrap_content" > <requestFocus /> </EditText> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="ç¡®å®" android:onClick="submit" /> <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="ç¡®å®ä½¿ç¨Bundle" android:onClick="submitByBundle" /> </LinearLayout>
<?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" > <TextView android:id="@+id/t2" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
å«å¿äºæ³¨å第äºä¸ªActivity
<!-- 注åActivity02 --> <activity android:name=".Activity02"></activity>
2.å°±æ¯å¨ä¸ä¸ªActivityä¸åæ¢ä»ä»¬çå¸å±æ件æ¶é´ï¼è¿æ ·å¯ä»¥ååä¸ç±»å ±äº«æ°æ®
setContentView(å¸å±æ件1);å¨ç¹å»äºä»¶ä¸å å ¥setContentView(å¸å±æ件2);
public class ActivityTest2Activity extends Activity { private String name=""; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main01); } public void submit(View view){ //å¨å¸å±1ä¸è·å¾ä½ è¾å ¥çå 容并ä¿å¨nameä¸ EditText et=(EditText)findViewById(R.id.t1); name=et.getText().toString(); //æå½åActivityå¸å±è®¾ç½®ä¸º02 setContentView(R.layout.main02); TextView tv=(TextView)findViewById(R.id.t2); tv.setText("ä½ è¾å ¥çååï¼"+name); } }
<?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" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="è¾å ¥ä¿¡æ¯ï¼" /> <EditText android:id="@+id/t1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="adasd" > <requestFocus /> </EditText> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="ç¡®å®" android:onClick="submit" /> </LinearLayout>
<?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" > <TextView android:id="@+id/t2" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
上一篇: ubuntu隐藏index.php的方法
下一篇: 谈谈PHP中的global全局变量