Android实例 每日一句(英语)
短信预约 -IT技能 免费直播动态提醒
前言:添加了学习模块,所以做了一个每日一句的功能,主要是在天行数据申请了一个每日一句的api,之后用okgo,调取,并解析成对象。获取文字、图片、音频等。在线显示、播放。同时做了一个底部弹出日历选择。
效果图:
implementation 'com.lzy.net:okgo:3.0.4'
implementation 'com.prolificinteractive:material-calendarview:1.4.3'
Json 格式:
{
"code": 200,
"msg": "success",
"newslist": [
{
"id": 3725,
"content": "Passion is a key ingredient to the study and practice of law and of life.",
"note": "激情是学习和实践法律不可或缺的要素,同时也是生命中不可或缺的要素。",
"tts": "https://edu-wps.ks3-cn-beijing.ksyun.com/audio/17c662155203497977f9536ac2bac6e1.mp3",
"imgurl": "https://edu-wps.ks3-cn-beijing.ksyun.com/image/88a1b7e9b02be6388329a4be911f7293.png",
"date": "2020-04-06"
}
]
}
网络获取:
public class HttpUtils {
public static final String TAG = "HttpUtils";
public static void getEnglishSentence(Context context, String date , StringCallback callback) throws IOException {
OkGo.get(UrlsConfig.EVERYDAY_ENGLISH_API)
.tag(context)
.params("key", ApiConfig.API_AppKey)
.params("date", date)
.execute(callback);
}
}
EverydayEnglishFragment.java
public class EverydayEnglishFragment extends BaseFragment {
public static final String TAG = "EverydayEnglishFragment";
@BindView(R.id.tv_year)
TextView tv_year;
@BindView(R.id.tv_month)
TextView tv_month;
@BindView(R.id.tv_day)
TextView tv_day;
@BindView(R.id.calendar_left)
ImageView calendar_left;
@BindView(R.id.calendar_right)
ImageView calendar_right;
@BindView(R.id.img_english)
SimpleDraweeView imgEnglish;
private Calendar calendar;
EnglishSentence.NewslistBean mNewslistBean;
public static Fragment getInstance() {
Fragment fragment = new EverydayEnglishFragment();
return fragment;
}
@Override
protected int contentViewID() {
return R.layout.fragment_everyday_english;
}
@Override
protected void initialize() {
EventBus.getDefault().register(this);
calendar = Calendar.getInstance();
setTextDate(calendar);
getHttpData(calendar);
return;
}
private void setTextDate(Calendar calendar) {
tv_year.setText(calendar.get(Calendar.YEAR) + "");
tv_month.setText(calendar.get(Calendar.MONTH) + 1 + "");
tv_day.setText(calendar.get(Calendar.DAY_OF_MONTH) + "");
changeArrowImage(calendar);
}
private void getHttpData(Calendar calendar) {
String date = FormatUtils.getDateTimeString(calendar.getTime(), FormatUtils.template_Date);
try {
StringCallback callback = new StringCallback() {
@Override
public void onSuccess(Response response) {
try {
String json = response.body().toString();
JSONObject jsonObject = new JSONObject(json);
int code = jsonObject.getInt("code");
if (code == 200) {
JSONArray jsonArray = jsonObject.getJSONArray("newslist");
List newslistBeanList = new Gson().fromJson(jsonArray.toString(),
new TypeToken<List>() {
}.getType());
LogUtils.d(TAG, newslistBeanList.get(0).toString());
mNewslistBean = newslistBeanList.get(0);
setTextImg(mNewslistBean);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onError(Response response) {
super.onError(response);
}
};
HttpUtils.getEnglishSentence(getContext(), date, callback);
} catch (IOException e) {
e.printStackTrace();
}
}
public void changeArrowImage(Calendar calendar){
if (DateUtils.isTody(calendar)){
calendar_left.setImageResource(R.mipmap.arrow_left);
calendar_right.setImageResource(R.mipmap.right_gay);
} else {
calendar_left.setImageResource(R.mipmap.arrow_left);
calendar_right.setImageResource(R.mipmap.arrow_right);
}
}
private void setTextImg(EnglishSentence.NewslistBean newslistBean) {
if (newslistBean.getImgurl() != null){
imgEnglish.setImageURI(newslistBean.getImgurl());
}
}
@OnClick({R.id.english_mediaPlayer, R.id.calendar_left, R.id.calendar_right, R.id.calender_select})
public void onClick(View view) {
switch (view.getId()) {
case R.id.english_mediaPlayer:
if (mNewslistBean == null){
return;
}
onPlayer(mNewslistBean);
break;
case R.id.calendar_left:
calendar.add(Calendar.DATE, -1);
setTextDate(calendar);
getHttpData(calendar);
break;
case R.id.calendar_right:
if (DateUtils.isTody(calendar)){
return;
}
calendar.add(Calendar.DATE, 1);
setTextDate(calendar);
getHttpData(calendar);
break;
case R.id.calender_select:
startActivity(CalenderSelectActivity.class);
getActivity().overridePendingTransition(R.anim.bottom_in,R.anim.bottom_silent);
break;
default:
}
}
private void onPlayer(EnglishSentence.NewslistBean newslistBean) {
if (newslistBean.getTts() != null) {
try {
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(newslistBean.getTts());
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEvent(CalenderEvent calenderEvent) {
switch (calenderEvent.getMessage()) {
case ConstantConfig.CALENDER_SELECT:
calendar = calenderEvent.getCalendar();
setTextDate(calendar);
getHttpData(calendar);
break;
default:
}
}
@Override
public void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
OkGo.getInstance().cancelAll();
}
}
日历选择
public class CalenderSelectActivity extends BaseActivity implements OnDateSelectedListener {
@BindView(R.id.materialCalendarView)
MaterialCalendarView widget;
@Override
protected int contentViewID() {
return R.layout.activity_calender_select;
}
@Override
protected void initialize() {
StatusBarUtils.setTransparent(this);
getSupportActionBar().hide();
setTopTitle("",false);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
widget.setSelectedDate(CalendarDay.today());
widget.state().edit().setMaximumDate(CalendarDay.today()).commit();
widget.setOnDateChangedListener(this);
}
@Override
public void onDateSelected(@NonNull MaterialCalendarView widget, @NonNull CalendarDay date, boolean selected) {
EventBus.getDefault().post(new CalenderEvent(ConstantConfig.CALENDER_SELECT,date.getCalendar()));
finish();
}
@Override
public void finish() {
super.finish();
overridePendingTransition(R.anim.bottom_silent,R.anim.bottom_out);
}
}
弹入 弹出 动画XML bottom_out.xml
bottom_silent.xml
作者:江南233
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341