Android Studio 用匿名类的方式实现一个按钮事件--使用Toast随机

用匿名类的方式实现一个按钮事件。定义一个字符串数组资源,包含若干个中国城市名称。点击按钮时用Toast随机显示字符串数组资源中的一个城市名。要求:
(1) 重新设计Toast的布局,比如家伙是那个颜色选项或者改变字体大小等。
(2) 让Toast显示在屏幕上方。
编辑文件:
a c t i v i t y _ m a i n . x m l activity\_main.xml activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="137dp"
        android:layout_marginLeft="137dp"
        android:layout_marginTop="400dp"
        android:layout_marginEnd="138dp"
        android:layout_marginRight="138dp"
        android:text="点击随机选择城市"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

M a i n A c t i v i t y . j a v a MainActivity.java MainActivity.java

package com.example.a171228356;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Html;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Random;

public class MainActivity extends Activity{
    private Button button1 = null;

    void init_view(){
        button1= (Button) findViewById(R.id.button1);
        //匿名类实现监听
        button1.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String[] city=new String[]{"","北京","上海","武汉","南京","南昌","深圳","长沙","广州","昆明","贵阳","哈尔滨","吉林","苏州","杭州","郑州","青岛","石家庄","沈阳","福州","济南","成都","兰州","台北","南宁","银川","太原","长春","合肥","海口","西安","西宁","呼和浩特","拉萨","乌鲁木齐"};
                        int str_len=city.length-1;
                        int pos=(int)(1+Math.random()*(str_len-1+1));
                        String choose=city[pos];

                        Random random = new Random();
                        int ransize=10|random.nextInt(100);
                        String ransize_str=ransize+"";
                        String choose_city="<font color='#000000'>"+choose+"</font>";//设定字体颜色

                        Toast toast= Toast.makeText(MainActivity.this, Html.fromHtml(choose_city),Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.TOP,-50,300);
                        LinearLayout layout = (LinearLayout)toast.getView();
                        TextView Tv =(TextView)layout.getChildAt(0);
                        Tv.setTextSize(ransize);//随机设置字体大小
                        int ranColor = 0xff000000 | random.nextInt(0x00ffffff);
                        layout.setBackgroundColor(ranColor);//随机背景颜色

                        toast.show();
                    }
                }
        );
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_main);
        init_view();
    }
}

仿真结果:
在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值