添加镜像依赖
在根部录下的build.gradle文件下添加镜像依赖,分别在 buildscript->repositories和allprojects->repositories节点下添加依赖
buildscript {
repositories {
//添加镜像依赖
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
maven{ url 'http://maven.aliyun.com/nexus/content/repositories/jcenter'}
maven{ url 'http://maven.aliyun.com/repository/google'}
google()
jcenter()
}
allprojects {
repositories {
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
maven{ url 'http://maven.aliyun.com/nexus/content/repositories/jcenter'}
maven{ url 'http://maven.aliyun.com/repository/google'}
google()
jcenter()
}
}
RecylerView显示数据
MainActivity代码
package com.json.test;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Adapter;
import android.widget.LinearLayout;
import java.util.ArrayList;
import java.util.HashMap;
public class RecyclerView extends AppCompatActivity
{
private androidx.recyclerview.widget.RecyclerView RecyV;
private ArrayList<HashMap<String, String>> adapterList = new ArrayList<>();//adapter列表
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycler_view);
iniView();
iniData();
iniEvent();
}
private void iniView()
{
RecyV = findViewById(R.id.RecyV);
/**
* 设置布局方式,这里显示的是列表方式,也可以设置GridView方式
*/
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
RecyV.setLayoutManager(linearLayoutManager);
/**
* 设置RecyclerView的列表高度不会自动变化
*/
RecyV.setHasFixedSize(true);
}
private void iniData()
{
//添加数据
for (int i = 0; i < 1000; i++)
{
HashMap<String, String> map = new HashMap<>();
map.put("tvName", "冯家振");
map.put("tvAge", String.valueOf(i));
adapterList.add(map);
//数据添加到适配器里面
MyAdapter myadapter = new MyAdapter(this, adapterList);
RecyV.setAdapter(myadapter);
}
}
private void iniEvent()
{
}
}
adapter布局文件
<?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="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tvAge"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="晏传利" />
</LinearLayout>
MyAdapter文件
package com.json.test;
import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import org.w3c.dom.Text;
import java.util.ArrayList;
import java.util.HashMap;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>
{
private ArrayList<HashMap<String, String>> adapterList = null;
private Activity activity;
public MyAdapter(Activity activity, ArrayList<HashMap<String, String>> adapterList)
{
this.activity = activity;
this.adapterList = adapterList;
Log.i("Jason", "我是数据列表个数"+adapterList.size());
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
{
ViewHolder viewHolder = new ViewHolder(LayoutInflater.from(activity).inflate(R.layout.adapter, parent, false));
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position)
{
holder.bindData(adapterList.get(position));
Log.i("Jason",adapterList.get(position).toString());
}
@Override
public int getItemCount()
{
return adapterList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder
{
private TextView tvName;
private TextView tvAge;
public ViewHolder(@NonNull View itemView)
{
super(itemView);
tvName = itemView.findViewById(R.id.tvName);
tvAge = itemView.findViewById(R.id.tvAge);
}
public void bindData(HashMap<String, String> map)
{
tvName.setText(map.get("tvName").toString());
Log.i("Jason","我被调用了");
tvAge.setText(map.get("tvAge").toString());
}
}
}
Gradle命令
1、查看当前gradle的版本号 .\gradlew.bat -v
2、编译项目 .\gradlew.bat build
3、清理项目 .\gradlew.bat clean
4、重新编译项目 .\gradlew.bar clean && .\gradlew build
5、安装项目 .\gradlew.bat app(项目名称):installDebug
6、卸载项目 .\gradlew.bat app(项目的名称):uninstallDebug
Activity去除标题,全屏显示
首先在theme中继承基础主题,然后在修改我们样式
Theme.xml文件
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
<!-- 自定义主题,继承基础主题-->
<style name="AppTheme.NoActionBar">
<!-- 去除ActionBar-->
<item name="windowActionBar">false</item>
<!-- 去除title-->
<item name="windowNoTitle">true</item>
</style>
</resources>
如果我们不需要全部的activity都适应我们自定义的主题,我们可以给activity单独指定Theme
例如:在AndroidManifest.xml文件中
<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">
<!-- 上面的Theme是指定全局的样式-->
<!-- 下面的Theme是给activity单独指定主题样式-->
<activity android:name=".activity.SplashActivity"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".activity.MainActivity">
</activity>
</application>
设置activity全屏显示
/**
* View.SYSTEM_UI_FLAG_HIDE_NAVIGATION 隐藏导航栏
* View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY 时虚拟按键半透明显示
* View.SYSTEM_UI_FLAG_FULLSCREEN 全屏显示
*/
View decorView = getWindow().getDecorView();
if (Build.VERSION.SDK_INT >= 19)
{
int options = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(options);
} else if (Build.VERSION.SDK_INT > 11 || Build.VERSION.SDK_INT < 19)
{
decorView.setSystemUiVisibility(View.GONE);
}
利用handler实现倒计时功能
public class SplashActivity extends AppCompatActivity
{
//定义一个handler
Handler handler=new Handler(){
@Override
public void handleMessage(@NonNull Message msg)
{
super.handleMessage(msg);
if(msg.what==0X001)
{
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
SplashActivity.this.finish();
}
}
};
private TextView tvDowntime;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
//postDelayed延时发送需要两个参数,
//第一个参数:是Runnable线程,第二个参数:是延时多长时间
handler.postDelayed(new Runnable() {
@Override
public void run()
{
//发送空消息
handler.sendEmptyMessage(0X001);
}
},3000);
}
}
SharedPreferences文件
利用SharePreferences保存一些小数据
保存
//第一个参数:文件的名称
//第二个参数:表示文件刻度了类型
//MODE_PRIVATE 私有的,只能自己读写;
//MODE_WORLD_READABLE //可以被其他程序读
//MODE_WORLD_WRITEABLE 可以被其他程序写
SharedPreferences preferences=getSharedPreferences("MyCloudMusic", MODE_PRIVATE);
SharedPreferences.Editor editor=preferences.edit();
editor.putString("name","冯家振");
editor.apply();//把键值对写入文件中
读取
SharedPreferences preferences=getSharedPreferences("MyCloudMusic", MODE_PRIVATE);
//如果文件中没有Key;“name”,则用“123”代替
text.setText(preferences.getString("name","123"));
查看应用私有目录
1、进入:adb shell
2、进入私有目录:run-ad com.json.mycloudmusic(应用完整包名)
3、打开文件:cat 文件名
向Fragment传递数据并取出
向Fragment中传递数据,利用单例的方式进行传递
/**
* 创建单例,并且传递参数
* @param id 图片的ID
* @return
*/
public static GuideFragment getInstance(int id)
{
if(fragment ==null)
{
fragment =new GuideFragment();
}
//创建一个Bundle
Bundle bundle=new Bundle();
bundle.putInt(Constant.ID,id);
//将Bundle设置到fragment
fragment.setArguments(bundle);
return fragment;
}
取出数据
//此方法在onCrateView执行完之后在执行,也就是说fragment的布局已经创建完成
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState)
{
super.onViewCreated(view, savedInstanceState);
iv=view.findViewById(R.id.iv);
//取出传递的数据
int imageID= getArguments().getInt(Constant.ID);
iv.setImageResource(imageID);
}
省略findViewByID以及点击事件
我们利用开源框架ButterKnife实现
1.添加依赖
//提供注解的方法找控件
//以及绑定方法
//https://github.com/JakeWharton/butterknife
implementation 'com.jakewharton:butterknife:10.2.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.0'
初始化
//初始化ButterKnife
ButterKnife.bind(this);
寻找控件
/**
* 登录按钮
*
* 字段不能申明为private
*/
@BindView(R.id.bt_login)
Button bt_login;
绑定点击事件
/**
* 登录按钮点击
*/
@OnClick(R.id.bt_login)
public void bt_login() {
startActivity(LoginActivity.class);
}