日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

不停的切换颜色

發布時間:2025/4/5 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 不停的切换颜色 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、第一步當然是要創建我們的項目,忽略!(如果初學請參照上一次內容)

1、我們今天要學習的式幀式布局,通過切換顏色案例來講解,主要式要事項點擊開始,三個文本框就不停的切換顏色,點擊暫停就停止切換顏色,概念模糊以下圖為參照理解:

二界面設計

1、在xml文件中寫入代碼,實現三個不同大小的框重疊,添加開始和暫停按鈕代碼如下

<?xml version="1.0" encoding="utf-8"?>

<FrameLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"><TextViewandroid:id="@+id/tvBottom"android:layout_width="300dp"android:layout_height="300dp"android:layout_gravity="center"android:background="#ff0000"android:text="@string/bottom"android:textColor="#ffff00"android:textSize="30sp" /><TextViewandroid:id="@+id/tvMiddle"android:layout_width="200dp"android:layout_height="200dp"android:layout_gravity="center"android:background="#0000ff"android:text="@string/middle"android:textColor="#ffff00"android:textSize="30sp" /><TextViewandroid:id="@+id/tvTop"android:layout_width="100dp"android:layout_height="100dp"android:layout_gravity="center"android:background="#00ff00"android:text="@string/top"android:textColor="#ffff00"android:textSize="30sp" /> </FrameLayout><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"><Buttonandroid:id="@+id/btnStart"android:layout_width="100dp"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:layout_marginRight="25dp"android:onClick="doSwitchColor"android:text="@string/start"android:textSize="20sp" /><Buttonandroid:id="@+id/btnPause"android:layout_width="100dp"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:layout_marginLeft="25dp"android:onClick="doSwitchColor"android:text="@string/pause"android:textSize="20sp" /></LinearLayout>

效果圖
2、字符串文件

三、實現功能(點擊開始就循環切換顏色,暫停就停止切換顏色)


附上代碼:
package net.lbd.switchcolor;

import androidx.appcompat.app.AppCompatActivity;
//導入要用到的模塊
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
private TextView tvBottom;
private TextView tvMiddle;
private TextView tvTop;
private int clickCount;
private int[] colors;

@Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// 利用布局資源文件設置用戶界面setContentView(R.layout.activity_main);// 通過資源標識獲得控件實例tvBottom = findViewById(R.id.tvBottom);tvMiddle = findViewById(R.id.tvMiddle);tvTop = findViewById(R.id.tvTop);

// }
// /**
// * 切換顏色單擊事件處理方法
// *
// * @param view
// */
// public void doSwitchColor(View view) {
// // 累加按鈕單擊次數
// clickCount++;
// // 單擊次數對3求余
// clickCount = clickCount % 3;
// // 判斷次數是0、1、2
// switch (clickCount) {
// case 0:
// // 紅——藍——綠
// colors = new int[]{Color.RED, Color.BLUE, Color.GREEN};
// break;
// case 1:
// // 藍——綠——紅
// colors = new int[]{Color.BLUE, Color.GREEN, Color.RED};
// break;
// case 2:
// // 綠——紅——藍
// colors = new int[]{Color.GREEN, Color.RED, Color.BLUE};
// break;
// }

//修改顏色切換的算法// 初始化顏色數組colors = new int[] {Color.RED, Color.BLUE, Color.GREEN}; }/*** 切換顏色單擊事件處理方法** @param view*/ public void doSwitchColor(View view) {// 切換顏色int temp = colors[0];colors[0] = colors[1];colors[1] = colors[2];colors[2] = temp;// 設置三層標簽的顏色tvBottom.setBackgroundColor(colors[0]);tvMiddle.setBackgroundColor(colors[1]);tvTop.setBackgroundColor(colors[2]); }

}

四、總結

今天重點有倆個,第一是幀式布局,二十是我們上此 講的事件處理,而且這次的事件處理和上次的也有所不同,雖然都同樣的 原理但是用的東西是不一樣的,這是布局的主要特點在于

總結

以上是生活随笔為你收集整理的不停的切换颜色的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。