返回系统HOME桌面

来源:互联网 发布:数据库存储图片路径 编辑:程序博客网 时间:2024/06/10 02:40

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() {@Overridepublic void onClick(View v) {Intent intent = new Intent();intent.setAction(Intent.ACTION_MAIN);//设置Intent动作intent.addCategory(Intent.CATEGORY_HOME);//设置Intent种类startActivity(intent);//将Intent传递给Activity}});    }}


0 0
原创粉丝点击