SayHello:
MainActivity.java
两种打开新的Activity的方法:传参和不传参
第一种方法:
import android.os.Bundle; import android.app.Activity; import android.text.Editable; import android.util.Log; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { private Button btnClick; private EditText etName; public static final String TAG = "MainActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnClick = (Button) findViewById(R.id.btnClick); etName = (EditText) findViewById(R.id.etName); btnClick.setOnClickListener(new ClickHandler()); } class ClickHandler implements OnClickListener { @Override public void onClick(View v) { String name = etName.getText().toString(); // Log.d(TAG,"Hello"+name); Toast.makeText(MainActivity.this, "你好" + name, Toast.LENGTH_LONG) .show(); } } }
</pre><pre name="code" class="html">第二种方法:
<pre name="code" class="html"><pre name="code" class="html"> import android.os.Bundle; import android.app.Activity; import android.text.Editable; import android.util.Log; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { private EditText etName; public static final String TAG = "MainActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); etName = (EditText) findViewById(R.id.etName); } public void sayHello(View view){ String name = etName.getText().toString(); System.out.println("nihao"); Log.d(TAG, "nihao"); Log.d(TAG,"hello"+view.getId()); Toast.makeText(MainActivity.this, "Hello" + name, Toast.LENGTH_LONG) .show(); } }
</pre><span style="white-space:pre"> </span>activity_main.xml<p></p><p><span style="white-space:pre"></span></p><pre name="code" class="html"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/bg" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:id="@+id/tvNamePrompt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="130dp" android:text="@string/tvNamePrompt" android:textSize="20sp" /> <EditText android:id="@+id/etName" android:drawableLeft="@drawable/name" android:drawablePadding="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/tvNamePrompt" android:layout_below="@id/tvNamePrompt" android:layout_marginTop="25dp" android:ems="8" > <requestFocus /> </EditText> <Button android:id="@+id/btnClick" android:background="@null" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/etName" android:layout_centerHorizontal="true" android:layout_marginTop="24dp" android:text="@string/btnClick" /> </RelativeLayout>
</pre><span style="white-space:pre"></span>colors.xml<p></p><p></p><pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?> <resources> <color name="red">#ff0000</color> <color name="blue">#0000ff</color> </resources>
</pre><span style="white-space:pre"> </span>dimens.xml<p></p><p></p><pre name="code" class="html"><resources> <!-- Default screen margins, per the Android Design guidelines. --> <dimen name="activity_horizontal_margin">16dp</dimen> <dimen name="activity_vertical_margin">16dp</dimen> </resources>
</pre><pre name="code" class="html"><span style="white-space:pre"> </span>strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">SayHello</string> <string name="action_settings">Settings</string> <string name="tvNamePrompt">请输入你的姓名</string> <string name="btnClick">请点击我</string> </resources>