1、布局文件
<LinearLayout 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:orientation="vertical">
<Button
android:id="@+id/home_button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="返回HOME"
android:textColor="@android:color/black"/>
</LinearLayout>
2、MainActivity类
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button home = (Button)findViewById(R.id.home_button1);
home.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);//设置Intent动作
intent.addCategory(Intent.CATEGORY_HOME);//设置Intent种类
startActivity(intent);//将Intent传递给Activity
}
});
}
}