四、代码清单
//MainActivity.java
package com.example.activity_01; import android.os.Bundle; import android.app.Activity; import android.app.AlertDialog; import android.content.Intent; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.view.View.OnClickListener; public class MainActivity extends Activity { private EditText myTextEdit; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView myTextView=(TextView)findViewById(R.id.myTextView); myTextEdit=(EditText)findViewById(R.id.myEditText); Button myButton=(Button)findViewById(R.id.myButton); System.out.println("Begin"); myTextView.setText("Input your name!"); myButton.setText("confirm"); myButton.setOnClickListener(new ButtonListener()); } @Override public boolean onCreateOptionsMenu(Menu menu) { menu.add(0,1,1,R.string.exit); menu.add(0,2,2,R.string.about); // getMenuInflater().inflate(R.menu.activity_main, menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onMenuItemSelected(int featureId, MenuItem item) { // TODO Auto-generated method stub if(item.getItemId()==1) finish(); if(item.getItemId()==2) new AlertDialog.Builder(this) .setTitle("About") .setMessage("Powerd By Empty.") .show(); return super.onMenuItemSelected(featureId, item); } class ButtonListener implements OnClickListener{ @Override public void onClick(View view0) { // TODO Auto-generated method stub String val=myTextEdit.getText().toString(); //生成一个Intent对象 Intent intent=new Intent(); //设置传递的参数 intent.putExtra("val", val); //从Activity IntentTest跳转到Activity IntentTest01 intent.setClass(MainActivity.this,SecondActivity.class); //启动intent的Activity MainActivity.this.startActivity(intent); System.out.println("ClickMe"); } } }
SecondActivity.java
package com.example.activity_01; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.TextView; public class SecondActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.second_layout); Intent intent=getIntent(); String value=intent.getStringExtra("val"); TextView et=(TextView)findViewById(R.id.textView1); et.setText("Hello,"+value); } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/myTextView" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <EditText android:id="@+id/myEditText" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <Button android:id="@+id/myButton" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
sencond_layout.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" > <TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20sp" > </TextView> </LinearLayout>
别忘了在manifest里面将Activity注册,不然会报错。
<activity android:name="SecondActivity" android:label="@string/app_name" android:screenOrientation="landscape" android:windowSoftInputMode="adjustPan"> </activity>
Android developers:http://developer.android.com/training/basics/firstapp/running-app.html google的官方教程,非常推荐。
雷一的博客:http://www.cnblogs.com/rayee/tag/Android/ 素然只有四篇,但作为入门教程还是很不错的。