初学者的作业
效果展示
1.可点击登录,注册按钮
2,已有密码,实现登录,或点击注册,可设定密码
3.第一个子页面,实现轮播图
4.第二个子页面实现音频播放,点击按钮可以切个
5.第三个子页面可实现选择照片或者拍照,点击按钮进行选择,图片显示在下方
6.第四个子页面,实现视频播放
未解决的部分:
1;直接点击注册无法实现转入登录输密码的界面
2:轮播图一直在播放第一张图
3:视频只有一个,无法实现多个视频的播放
所以要继续加油!!
部分Java代码
1.fragmentone
package com.example.bigwork;
import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.net.Uri;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class Fragmentone extends Fragment {
private View mView;
private ViewPager mViewPaper;
private List<ImageView> images;
private List<View> dots;
private int currentItem;
//记录上一次点的位置
private int oldPosition = 0;
//存放图片的id
private int[] imageIds = new int[]{
R.drawable.att_drawable,
R.drawable.att_drawable,
R.drawable.att_drawable,
R.drawable.att_drawable,
R.drawable.att_drawable
};
private String[] titles = new String[]{
"轮播1",
"轮播2",
"轮播3",
"轮播4",
"轮播5"
};
private TextView title;
private ViewPagerAdapter adapter;
private ScheduledExecutorService scheduledExecutorService;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mView=inflater.inflate(R.layout.fragment_fragmentone, null);
setView();
return mView;
}
private OnFragmentInteractionListener mListener;
public interface OnFragmentInteractionListener {
void onFragmentInteraction();
}
private void setView() {
mViewPaper = (ViewPager)mView.findViewById(R.id.vp);
//显示的图片
images = new ArrayList<ImageView>();
for(int i = 0; i < imageIds.length; i++){
ImageView imageView = new ImageView(getActivity());
imageView.setBackgroundResource(imageIds[i]);
images.add(imageView);
}
dots = new ArrayList<View>();
dots.add(mView.findViewById(R.id.dot_0));
dots.add(mView.findViewById(R.id.dot_1));
dots.add(mView.findViewById(R.id.dot_2));
dots.add(mView.findViewById(R.id.dot_3));
dots.add(mView.findViewById(R.id.dot_4));
title = (TextView) mView.findViewById(R.id.title);
title.setText(titles[0]);
ViewPagerAdapter adapter = new ViewPagerAdapter();
mViewPaper.setAdapter(adapter);
mViewPaper.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
title.setText(titles[position]);
dots.get(position).setBackgroundResource(R.drawable.xyd);
dots.get(oldPosition).setBackgroundResource(R.drawable.yd);
oldPosition = position;
currentItem = position;
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
public class ViewPagerAdapter extends PagerAdapter {
@Override
public int getCount() {
return images.size();
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
@Override
public void destroyItem(ViewGroup view, int position, Object object) {
view.removeView(images.get(position));
}
@Override
public Object instantiateItem(ViewGroup view, int position) {
// TODO Auto-generated method stub
view.addView(images.get(position));
return images.get(position);
}
}
/**
* 利用线程池定时执行动画轮播
*/
@Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
scheduledExecutorService.scheduleWithFixedDelay(
new ViewPageTask(),
2,
2,
TimeUnit.SECONDS);
}
/**
* 图片轮播任务
* @author liuyazhuang
*
*/
private class ViewPageTask implements Runnable{
@Override
public void run() {
currentItem = (currentItem + 1) % imageIds.length;
mHandler.sendEmptyMessage(0);
}
}
/**
* 接收子线程传递过来的数据
*/
private Handler mHandler = new Handler(){
public void handleMessage(android.os.Message msg) {
mViewPaper.setCurrentItem(currentItem);
};
};
@Override
public void onStop() {
// TODO Auto-generated method stub
super.onStop();
if(scheduledExecutorService != null){
scheduledExecutorService.shutdown();
scheduledExecutorService = null;
}
}
}
2.fragmentfour
package com.example.bigwork;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.VideoView;
public class Fragmentfour extends Fragment {
private VideoView videoView;
private MediaController mediaController;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_fragmentfour, container, false);
videoView=view.findViewById(R.id.sp_bf);
mediaController =new MediaController(getContext());
String uri_phath="android.resource://com.example.bigwork/"+R.raw.video;
Uri uri=Uri.parse(uri_phath);
videoView.setVideoURI(uri);
videoView.setMediaController(mediaController);
return view;
}
}
3.main2activity(登录输密码的)
package com.example.bigwork;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Main2Activity extends AppCompatActivity implements View.OnClickListener{
private SharedPreferences sp;
private SharedPreferences.Editor editor;
private EditText account;
private EditText password;
private Button button3;
private Button button7;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
init();
sp=getSharedPreferences("data",MODE_PRIVATE);
editor=sp.edit();
}
private void init() {
account=findViewById(R.id.account);
password=findViewById(R.id.password);
button3=findViewById(R.id.button3);
button7=findViewById(R.id.button7);
button3.setOnClickListener(this);
button7.setOnClickListener(this);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data){
super.onActivityResult(requestCode,resultCode,data);
switch (requestCode){
case 1:
editor.putString("账号",data.getStringExtra("账号"));
editor.putString("密码",data.getStringExtra("密码"));
editor.apply();
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button3:
if (account.getText().toString().equals(sp.getString("账号", ""))) {
if (password.getText().toString().equals(sp.getString("密码", ""))) {
Toast.makeText(Main2Activity.this, "登陆成功", Toast.LENGTH_LONG).show();
// Intent intent1 = new Intent(Main2Activity.this, Main4Activity.class);
startActivity(new Intent(Main2Activity.this,Main4Activity.class));
} else {
Toast.makeText(Main2Activity.this, "账号或密码错误", Toast.LENGTH_LONG).show();
}
}
break;
case R.id.button7:
Intent intent2 = new Intent(Main2Activity.this, Main3Activity.class);
startActivityForResult(intent2, 1);
}
}
}
勇敢大林,不怕困难,加油