整体运行效果展示
MainActivity.java:
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 {
public Button btnClick;
public 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,
"Hello"+name, Toast.LENGTH_LONG).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
activity_main.xml:
<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>
string.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>