1.4 创建第一个Android项目Toast

使用AndroidStudio开发安卓应用大致分为三步:
1、创建一个Android项目或者Android模块
2、在XML布局文件中定义应用程序的用户界面
3、在Java代码中编写业务实现

activty_main.xml


    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/toast_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text= "@string/toastBtn"/>  注意此处的text字符串引用方式,这里的字符串预先在string中定义

    </LinearLayout>

strings.xml

<resources>
    <string name="app_name">Toast</string>
    <string name="toastBtn">弹窗</string>
    </resources>

MainActivity.java

package com.ryshine.toast;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {
    //创建Toast对象为null
    static Toast toast = null;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = findViewById(R.id.toast_btn);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastShow(MainActivity.this, "弹窗");
            }
        });
    }

    //这里弹窗的代码可以避免多次点击按钮,弹窗一直叠加时间弹出的问题
    //点击一次按钮,弹窗弹出,弹窗未消失时再次点击按钮,弹窗则重新设置时间与弹窗文本,不会产生弹窗时间叠加
    public void ToastShow(Context context, String s){
        //如果toast为null,说明之前没有为Toast对象初始化,即这是第一次对Toast操作
        if (toast == null){
            toast = Toast.makeText(context, s, Toast.LENGTH_SHORT);
        }else {
            //如果Toast对象已经存在,且有过操作,则重新设置String与弹窗时间
            toast.setText(s);
            toast.setDuration(Toast.LENGTH_SHORT);
        }
        //show()
        toast.show();
    }
}

AndroidMainfest.xml

安卓四大组件使用前都需要在AndroidMainfest中注册

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ryshine.toast">

    <application   注意这里的格式
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">   注意这里的格式
            <intent-filter>   注意这里的格式
                <action android:name="android.intent.action.MAIN"/>   注意这里的格式
                <category android:name="android.intent.category.LAUNCHER"/>  注意这里的格式
            </intent-filter>
        </activity>
    </application>  注意这里的格式

</manifest>

截图如下

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值