Android Studio App开发中改造已有的控件实战(包括自定义支付宝月份选择器、给翻页栏添加新属性、不滚动的列表视图 附源码)

简介: Android Studio App开发中改造已有的控件实战(包括自定义支付宝月份选择器、给翻页栏添加新属性、不滚动的列表视图 附源码)

需要全部源码和图片集请点赞关注收藏后评论区留言~~~

一、自定义月份选择器

虽然Android提供了许多控件,但是仍然不够用,比如系统自带日期选择器和时间选择器,但是却没有月份选择器,倘若选择某个月份怎么办呢?这时候就要我们自己定义

其实做法很简单,就是去掉右侧的日子列表就是月份列表了,此处只对下拉框生效 效果如下

重新包装后的月份选择器继承了日期选择器的所有方法,而且控件界面与支付宝的一模一样

代码如下

Java类

package com.example.chapter10;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.example.chapter10.widget.MonthPicker;
@SuppressLint("DefaultLocale")
public class MonthPickerActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView tv_month;
    private MonthPicker mp_month; // 声明一个月份选择器对象
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_month_picker);
        tv_month = findViewById(R.id.tv_month);
        // 从布局文件中获取名叫mp_month的月份选择器
        mp_month = findViewById(R.id.mp_month);
        findViewById(R.id.btn_ok).setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_ok) {
            // 获取月份选择器mp_month设定的年月
            String desc = String.format("您选择的月份是%d年%d月",
                    mp_month.getYear(), mp_month.getMonth() + 1);
            tv_month.setText(desc);
        }
    }
}

XML文件如下

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="5dp">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="请选择月份"
        android:textColor="@color/black"
        android:textSize="17sp" />
    <!-- 自定义的月份选择器,需要使用全路径 -->
    <com.example.chapter10.widget.MonthPicker
        android:id="@+id/mp_month"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:calendarViewShown="false"
        android:datePickerMode="spinner"
        android:gravity="center" />
    <Button
        android:id="@+id/btn_ok"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="确  定"
        android:textColor="@color/black"
        android:textSize="17sp" />
    <TextView
        android:id="@+id/tv_month"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:textColor="@color/black"
        android:textSize="17sp" />
</LinearLayout>

二、给翻页标签栏添加新属性

我们现在可以在现有控件上做假发,也就是给控件增加新的属性或者方法,如翻页标签栏里原本不能设置文本大小和文本颜色,对用户不够友好同时也不够美观,下面我们自定义属性来扩展它的功能。效果如下

可见我们在原来的基础上把标签栏文字的大小和颜色都进行了改变 更加美观

代码如下

Java类

package com.example.chapter10;
import android.os.Bundle;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager.widget.ViewPager.OnPageChangeListener;
import com.example.chapter10.adapter.ImagePagerAdapater;
import com.example.chapter10.bean.GoodsInfo;
import java.util.ArrayList;
public class CustomTabActivity extends AppCompatActivity implements OnPageChangeListener {
    private ArrayList<GoodsInfo> mGoodsList; // 手机商品列表
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_tab);
        mGoodsList = GoodsInfo.getDefaultList();
        // 构建一个商品图片的翻页适配器
        ImagePagerAdapater adapter = new ImagePagerAdapater(this, mGoodsList);
        // 从布局视图中获取名叫vp_content的翻页视图
        ViewPager vp_content = findViewById(R.id.vp_content);
        vp_content.setAdapter(adapter); // 设置翻页视图的适配器
        vp_content.setCurrentItem(0); // 设置翻页视图显示第一页
        vp_content.addOnPageChangeListener(this); // 给翻页视图添加页面变更监听器
    }
    // 翻页状态改变时触发。state取值说明为:0表示静止,1表示正在滑动,2表示滑动完毕
    // 在翻页过程中,状态值变化依次为:正在滑动→滑动完毕→静止
    public void onPageScrollStateChanged(int state) {}
    // 在翻页过程中触发。该方法的三个参数取值说明为 :第一个参数表示当前页面的序号
    // 第二个参数表示当前页面偏移的百分比,取值为0到1;第三个参数表示当前页面的偏移距离
    public void onPageScrolled(int position, float ratio, int offset) {}
    // 在翻页结束后触发。position表示当前滑到了哪一个页面
    public void onPageSelected(int position) {
        Toast.makeText(this, "您翻到的手机品牌是:" + mGoodsList.get(position).name, Toast.LENGTH_SHORT).show();
    }
}

XML文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <androidx.viewpager.widget.ViewPager
        android:id="@+id/vp_content"
        android:layout_width="match_parent"
        android:layout_height="360dp" >
        <!-- 这里使用自定义控件的全路径名称,其中textColor和textSize为自定义的属性 -->
        <com.example.chapter10.widget.CustomPagerTab
            android:id="@+id/pts_tab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:textColor="#ff0000"
            app:textSize="17sp" />
    </androidx.viewpager.widget.ViewPager>
</LinearLayout>

三、不滚动的列表视图

列表视图很多时候最后一次性展示完所有的行,这样用户看着比较明白直观,接下来我们编写一个扩展自ListView的不滚动列表视图NoScrollListView 效果如下

原视图展示内容太少,改进后信息呈现的更加清晰

代码如下

Java类

package com.example.chapter10;
import android.os.Bundle;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
import com.example.chapter10.adapter.PlanetListAdapter;
import com.example.chapter10.bean.Planet;
import com.example.chapter10.widget.NoScrollListView;
public class NoscrollListActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_noscroll_list);
        PlanetListAdapter adapter1 = new PlanetListAdapter(this, Planet.getDefaultList());
        // 从布局文件中获取名叫lv_planet的列表视图
        // lv_planet是系统自带的ListView,被ScrollView嵌套只能显示一行
        ListView lv_planet = findViewById(R.id.lv_planet);
        lv_planet.setAdapter(adapter1); // 设置列表视图的行星适配器
        lv_planet.setOnItemClickListener(adapter1);
        lv_planet.setOnItemLongClickListener(adapter1);
        PlanetListAdapter adapter2 = new PlanetListAdapter(this, Planet.getDefaultList());
        // 从布局文件中获取名叫nslv_planet的不滚动列表视图
        // nslv_planet是自定义控件NoScrollListView,会显示所有行
        NoScrollListView nslv_planet = findViewById(R.id.nslv_planet);
        nslv_planet.setAdapter(adapter2); // 设置不滚动列表视图的行星适配器
        nslv_planet.setOnItemClickListener(adapter2);
        nslv_planet.setOnItemLongClickListener(adapter2);
    }
}

XML文件

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="10dp"
            android:text="下面是系统自带的列表视图"
            android:textColor="#ff0000"
            android:textSize="17sp" />
        <ListView
            android:id="@+id/lv_planet"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:dividerHeight="1dp" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="10dp"
            android:text="下面是自定义的列表视图"
            android:textColor="#00ff00"
            android:textSize="17sp" />
        <!-- 自定义的不滚动列表视图,需要使用全路径 -->
        <com.example.chapter10.widget.NoScrollListView
            android:id="@+id/nslv_planet"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:dividerHeight="1dp" />
    </LinearLayout>
</ScrollView>

创作不易 觉得有帮助请点赞关注收藏~~~

相关文章
|
4天前
|
JSON 编译器 开发工具
VS Code阅读Android源码
VS Code阅读Android源码
11 1
|
24天前
|
XML Java Android开发
Android实现自定义进度条(源码+解析)
Android实现自定义进度条(源码+解析)
52 1
|
24天前
|
Java Android开发
Android反编译查看源码
Android反编译查看源码
25 0
|
4天前
|
移动开发 Java Unix
Android系统 自动加载自定义JAR文件
Android系统 自动加载自定义JAR文件
21 1
|
4天前
|
Shell Android开发 开发者
Android系统 自定义动态修改init.custom.rc
Android系统 自定义动态修改init.custom.rc
23 0
|
4天前
|
存储 安全 Android开发
Android系统 自定义系统和应用权限
Android系统 自定义系统和应用权限
19 0
|
4天前
|
Linux 开发工具 Android开发
Docker系列(1)安装Linux系统编译Android源码
Docker系列(1)安装Linux系统编译Android源码
7 0
|
28天前
|
Java Android开发
Android Studio的使用导入第三方Jar包
Android Studio的使用导入第三方Jar包
12 1
|
28天前
|
Android开发
Android 开发 pickerview 自定义选择器
Android 开发 pickerview 自定义选择器
12 0
|
1月前
|
定位技术 API 数据库
基于Android的在线移动电子导航系统的研究与实现(论文+源码)_kaic
基于Android的在线移动电子导航系统的研究与实现(论文+源码)_kaic