intentDemo_1

 

package com.test.intentdemo;
 
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
 
public class MainActivity extends Activity {
     
    private Button btn=null;
     
    public void onCreate(Bundle savedInstanceState)//onCreate方法用来初始化Activity实例对象
    {
        super.onCreate(savedInstanceState);//super.onCreate(savedInstanceState)的作用是调用其父类Activity的onCreate方法来实现对界面的图画绘制工作
        setContentView(R.layout.activity_main);//setContentView(R.layout.main)的作用是加载一个界面
        btn=(Button)findViewById(R.id.btn);
        btn.setOnClickListener(listener);
    }
    private View.OnClickListener listener=new View.OnClickListener()
    {
         
        @Override
        public void onClick(View v)
        {
            Intent intent=new Intent();
            intent.setAction(Intent.ACTION_SENDTO);
            intent.setData(Uri.parse("smsto:5554"));
            intent.putExtra("sms_body", "Hello!");//sms_body不能随意换成其他
            startActivity(intent);
             
        }
    };
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }    
}
好的,我来为您介绍如何在 Android Studio 中创建一个 Intent Demo。 1. 打开 Android Studio,选择 "Start a new Android Studio project"。 2. 在 "Configure your new project" 页面中,输入应用程序名称并选择项目保存的位置。然后,选择 "Phone and Tablet" 作为设备,选择 "Empty Activity" 作为模板。最后,单击 "Finish" 创建项目。 3. 在项目视图中,找到 "app" 文件夹并右键单击它。选择 "New" -> "Activity" -> "Empty Activity"。 4. 在 "Configure Activity" 页面中,输入 Activity 名称(例如:IntentDemoActivity),然后单击 "Finish"。 5. 打开 IntentDemoActivity.java 文件,添加以下代码: ```java Button button = findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(IntentDemoActivity.this, SecondActivity.class); startActivity(intent); } }); ``` 6. 在项目视图中,右键单击 "app" 文件夹,选择 "New" -> "Activity" -> "Empty Activity"。 7. 在 "Configure Activity" 页面中,输入 Activity 名称(例如:SecondActivity),然后单击 "Finish"。 8. 打开 SecondActivity.java 文件,添加以下代码: ```java Button button = findViewById(R.id.button2); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(SecondActivity.this, IntentDemoActivity.class); startActivity(intent); } }); ``` 9. 在 res/layout 文件夹中,打开 activity_intent_demo.xml 文件,并添加两个按钮: ```xml <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Go to Second Activity" android:layout_marginTop="16dp" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"/> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Go to First Activity" android:layout_marginTop="16dp" app:layout_constraintTop_toBottomOf="@id/button" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"/> ``` 10. 运行应用程序并单击第一个按钮,会跳转到 SecondActivity。再单击第二个按钮,会回到 IntentDemoActivity。 完成以上步骤后,您已经成功创建了一个简单的 Intent Demo
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值