Intent对象练习1---返回系统桌面

来源:互联网 发布:mac怎么玩守望先锋 编辑:程序博客网 时间:2024/06/09 15:45

 代码:

 activity_main.xml

<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_button"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="返回Home"         android:textColor="@android:color/black"/></LinearLayout>

MainActivity.java

package com.example.intentone;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;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_button);        home.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubIntent intent=new Intent();intent.setAction(Intent.ACTION_MAIN);//设置Intent动作intent.addCategory(Intent.CATEGORY_HOME);//设置Intent种类startActivity(intent);}});    }}

截图:




0 0
原创粉丝点击