Android TextSwitcher 开源项目教程

随笔1周前发布 感悟人生
19 0 0

Android TextSwitcher 开源项目教程

android-TextSwitcherThis sample has been deprecated/archived.项目地址:https://gitcode.com/gh_mirrors/an/android-TextSwitcher

1. 项目的目录结构及介绍




android-TextSwitcher/


├── app/


│   ├── build.gradle


│   ├── src/


│   │   ├── main/


│   │   │   ├── java/


│   │   │   │   └── com/


│   │   │   │       └── example/


│   │   │   │           └── textswitcher/


│   │   │   │               ├── MainActivity.java


│   │   │   ├── res/


│   │   │   │   ├── layout/


│   │   │   │   │   ├── activity_main.xml


│   │   │   │   ├── values/


│   │   │   │   │   ├── strings.xml


│   │   │   │   │   ├── styles.xml


├── build.gradle


├── settings.gradle

目录结构介绍

app/: 主应用程序模块。
build.gradle: 应用模块的构建脚本。src/: 源代码和资源文件。
main/: 主源集。
java/: Java 源代码。
com/example/textswitcher/: 示例包路径。
MainActivity.java: 主活动文件。 res/: 资源文件。
layout/: 布局文件。
activity_main.xml: 主活动布局文件。 values/: 值资源文件。
strings.xml: 字符串资源。styles.xml: 样式资源。 build.gradle: 项目级构建脚本。settings.gradle: 项目设置文件。

2. 项目的启动文件介绍

MainActivity.java




package com.example.textswitcher;


 


import android.app.Activity;


import android.os.Bundle;


import android.view.View;


import android.widget.TextSwitcher;


import android.widget.TextView;


import android.widget.ViewSwitcher;


 


public class MainActivity extends Activity {


 


    private TextSwitcher textSwitcher;


    private String[] texts = {"Text 1", "Text 2", "Text 3"};


    private int index = 0;


 


    @Override


    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);


        setContentView(R.layout.activity_main);


 


        textSwitcher = findViewById(R.id.textSwitcher);


        textSwitcher.setFactory(new ViewSwitcher.ViewFactory() {


            @Override


            public View makeView() {


                TextView textView = new TextView(MainActivity.this);


                textView.setTextSize(24);


                return textView;


            }


        });


 


        textSwitcher.setText(texts[index]);


    }


 


    public void nextText(View view) {


        index = (index + 1) % texts.length;


        textSwitcher.setText(texts[index]);


    }


}

文件介绍

MainActivity.java: 这是应用程序的主活动文件,负责初始化 TextSwitcher 并设置其工厂方法来创建 TextView。它还包含一个方法 nextText,用于切换到下一个文本。

3. 项目的配置文件介绍

build.gradle (项目级)




// 项目级构建脚本


buildscript {


    repositories {


        google()


        jcenter()


    }


    dependencies {


        classpath 'com.android.tools.build:gradle:4.1.0'


    }


}


 


allprojects {


    repositories {


        google()


        jcenter()


    }


}


 


task clean(type: Delete) {


    delete rootProject.buildDir


}

build.gradle (应用模块级)




// 应用模块级构建脚本


apply plugin: 'com.android.application'


 


android {


    compileSdkVersion 30


    defaultConfig {


        applicationId "com.example.textswitcher"


        minSdkVersion 16


        targetSdkVersion 30


        versionCode 1


        versionName "1.0"


    }


    buildTypes {


        release {

android-TextSwitcherThis sample has been deprecated/archived.项目地址:https://gitcode.com/gh_mirrors/an/android-TextSwitcher

© 版权声明

相关文章

暂无评论

您必须登录才能参与评论!
立即登录
暂无评论...