作业哒!
作业一 (4个按例和登录注册在一起做的)
mainActivity
package com.example.day12_13_week;
import android.Manifest;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.day12_13_week.entiy.HttpUtils;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileActivity extends AppCompatActivity {
private EditText editUser;
private EditText editPwd;
private CheckBox checkboxRemember;
private Button sendUserMsg;
private Button buttonLoadJson;
private Button buttonLoadImaage;
private TextView textJson;
private ImageView image_pic;
private String path = "http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=10&page=1";
private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_file);
initView();
}
private void initView() {
editUser = (EditText) findViewById(R.id.edit_user);
editPwd = (EditText) findViewById(R.id.edit_pwd);
checkboxRemember = (CheckBox) findViewById(R.id.checkbox_remembers);
sendUserMsg = (Button) findViewById(R.id.button_sendMsg);
buttonLoadJson = (Button) findViewById(R.id.button_loadJson);
buttonLoadImaage = (Button) findViewById(R.id.button_loadImaage);
textJson = (TextView) findViewById(R.id.text_json);
image_pic = (ImageView) findViewById(R.id.Image_pic);
SharedPreferences sp = getSharedPreferences("one", MODE_PRIVATE);
boolean isCheck = sp.getBoolean("isCheck", false);
if(isCheck){
String user = sp.getString("user", "");
editUser.setText(user);
checkboxRemember.setChecked(true);
}
sendUserMsg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(checkboxRemember.isChecked()){
String user = editUser.getText().toString();
String pwd = editPwd.getText().toString();
SharedPreferences sp = getSharedPreferences("one", MODE_PRIVATE);
SharedPreferences.Editor edit = sp.edit();
edit.putString("user",user);
edit.putString("pwd",pwd);
edit.putBoolean("isCheck",true);
edit.commit();
Toast.makeText(FileActivity.this, "登陆成功!", Toast.LENGTH_SHORT).show();
}else{
SharedPreferences sp = getSharedPreferences("one", MODE_PRIVATE);
SharedPreferences.Editor edit = sp.edit();
edit.remove("isCheck");
edit.remove("pwd");
edit.commit();
}
}
});
clickOne();
clickTwo();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
//设置请求码等于上面设置的并且 允许规则↓ == 包资源管理者的 允许授予↓
if(requestCode==1) {
if(grantResults[0] == PackageManager.PERMISSION_GRANTED) {
} else {
finish();
}
}else if(requestCode == 2){
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
} else {
finish();
}
}
}
private void clickTwo() {
//下载图片
buttonLoadImaage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE},2);
new Thread(new Runnable() {
@Override
public void run() {
final Bitmap s = HttpUtils.LoadImage("http:\\/\\/www.qubaobei.com\\/ios\\/cf\\/uploadfile\\/132\\/47\\/46968.jpg");
handler.post(new Runnable() {
@Override
public void run() {
image_pic.setImageBitmap(s);
}
});
}
}).start();
}
});
}
private void clickOne() {
//下载json
buttonLoadJson.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE},1);
new Thread(new Runnable() {
@Override
public void run() {
final String s = HttpUtils.LoadJson(path);
handler.post(new Runnable() {
@Override
public void run() {
textJson.setText(s);
File directory = Environment.getExternalStorageDirectory();
File file = new File(directory, "one.txt");
try {
FileOutputStream fos = new FileOutputStream(file);
fos.write(s.getBytes());
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}).start();
}
});
}
}
MainActivity2
package com.example.day12_13_week;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.example.day12_13_week.Fragment.TestFragment;
import java.util.ArrayList;
import java.util.List;
public class Main2Activity extends AppCompatActivity {
private ViewPager viewPager;
private LinearLayout layoutLine;
private List<String> dataList = new ArrayList<>();
private List<Fragment> fragmentList = new ArrayList<>();
private List<ImageView> imageViewList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
initData();
initView();
}
private void initData() {
for (int i = 0; i < 4; i++) {
TestFragment testFragment = new TestFragment();
Bundle bundle = new Bundle();
bundle.putString("text","这是第"+(i+1)+"个");
testFragment.setArguments(bundle);
fragmentList.add(testFragment);
ImageView imageView = new ImageView(this);
if(i == 0){
imageView.setImageResource(R.drawable.radio_circle_yes);
}else {
imageView.setImageResource(R.drawable.radio_circle);
}
imageViewList.add(imageView);
}
}
private void initView() {
viewPager = (ViewPager) findViewById(R.id.view_pager);
layoutLine = (LinearLayout) findViewById(R.id.layout_line);
for (int i = 0; i < imageViewList.size(); i++) {
layoutLine.addView(imageViewList.get(i));
}
//viewPager的适配器
viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int i) {
return fragmentList.get(i);
}
@Override
public int getCount() {
return fragmentList.size();
}
});
//viewPager的监听器
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i1) {
}
@Override
public void onPageSelected(int i) {
for (int i1 = 0; i1 < imageViewList.size(); i1++) {
if(i1 == i){
ImageView imageView = imageViewList.get(i1);
imageView.setImageResource(R.drawable.radio_circle_yes);
}else{
ImageView imageView = imageViewList.get(i1);
imageView.setImageResource(R.drawable.radio_circle);
}
}
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
}
}
MainActivity3
package com.example.day12_13_week;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private EditText editUser;
private EditText editPwd;
private CheckBox checkboxRemember;
private Button buttonLogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editUser = (EditText) findViewById(R.id.edit_user);
editPwd = (EditText) findViewById(R.id.edit_pwd);
checkboxRemember = (CheckBox) findViewById(R.id.checkbox_remember);
buttonLogin = (Button) findViewById(R.id.button_login);
SharedPreferences sp = getSharedPreferences("one", MODE_PRIVATE);
boolean isCheck = sp.getBoolean("isCheck", false);
if(isCheck){
editUser.setText(sp.getString("user",""));
editPwd.setText(sp.getString("pwd",""));
checkboxRemember.setChecked(true);
}
buttonLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(checkboxRemember.isChecked()) {
String user = editUser.getText().toString();
String pwd = editPwd.getText().toString();
SharedPreferences sp = getSharedPreferences("one", MODE_PRIVATE);
SharedPreferences.Editor edit = sp.edit();
edit.putString("user", user);
edit.putString("pwd", pwd);
edit.apply();
}else{
SharedPreferences sp = getSharedPreferences("one", MODE_PRIVATE);
SharedPreferences.Editor edit = sp.edit();
edit.putBoolean("isCheck",false);
edit.apply();
}
}
});
}
}
碎片Fragment
package com.example.day12_13_week.Fragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.day12_13_week.R;
import org.w3c.dom.Text;
/**
* A simple {@link Fragment} subclass.
*/
public class TestFragment extends Fragment {
public TestFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View inflate = inflater.inflate(R.layout.fragment_test, container, false);
Bundle bundle = getArguments();
TextView text = inflate.findViewById(R.id.text_test);
text.setText(bundle.getString("text",""));
return inflate;
}
}
作业二 (7月老冯-简单)
mainActivity中
package com.example.app2;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.app2.Fragment.FirstFragment;
import com.example.app2.Fragment.FourthFragment;
import com.example.app2.Fragment.SecondFragment;
import com.example.app2.Fragment.ThirdFragment;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
public class WelcomeActivity extends AppCompatActivity {
private TabLayout tabLayout;
private ViewPager pageView;
private LinearLayout layoutLine;
private TextView textTime;
private Button buttonSend;
private List<Fragment> fragmentList = new ArrayList<>();
private List<String> dataList = new ArrayList<>();
private List<ImageView> imageViewList = new ArrayList<>();
private Timer timer;
private int index = 5;
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what){
case 1:
index --;
textTime.setText("倒计时:"+index+"秒");
if(index == 0){
timer.cancel();
Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
initData();
initView();
}
private void initData() {
fragmentList.add(new FirstFragment());
fragmentList.add(new SecondFragment());
fragmentList.add(new ThirdFragment());
fragmentList.add(new FourthFragment());
dataList.add("新闻");
dataList.add("娱乐");
dataList.add("看点");
dataList.add("其他");
for (int i = 0 ; i < 4 ; i ++){
ImageView imageView = new ImageView(this);
if(i == 0){
imageView.setImageResource(R.drawable.radio_circle_yes);
}else{
imageView.setImageResource(R.drawable.radio_circle_no);
}
imageViewList.add(imageView);
}
}
private void initView() {
tabLayout = (TabLayout) findViewById(R.id.tab_layout);
pageView = (ViewPager) findViewById(R.id.page_view);
layoutLine = (LinearLayout) findViewById(R.id.layout_line);
textTime = (TextView) findViewById(R.id.text_time);
buttonSend = (Button) findViewById(R.id.button_send);
PagerAdapters();
PagerListener();
for (int i = 0; i < imageViewList.size(); i++) {
layoutLine.addView(imageViewList.get(i));
}
}
private void PagerListener() {
pageView.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i1) {
}
@Override
public void onPageSelected(int position) {
for (int i = 0; i < imageViewList.size(); i++) {
if(i == position){
ImageView imageView = imageViewList.get(i);
imageView.setImageResource(R.drawable.radio_circle_yes);
}else{
ImageView imageView = imageViewList.get(i);
imageView.setImageResource(R.drawable.radio_circle_no);
}
}
if(position == fragmentList.size()-1){
textTime.setVisibility(View.VISIBLE);
buttonSend.setVisibility(View.VISIBLE);
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
handler.sendEmptyMessage(1);
}
},0,1000);
buttonSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
});
}
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
}
private void PagerAdapters() {
pageView.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int i) {
return fragmentList.get(i);
}
@Override
public int getCount() {
return fragmentList.size();
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return dataList.get(position);
}
});
tabLayout.setupWithViewPager(pageView);
}
}
viewPager-第一个碎片
package com.example.app2.Fragment;
import android.Manifest;
import android.app.DatePickerDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Toast;
import com.alibaba.fastjson.JSON;
import com.example.app2.R;
import com.example.app2.entiy.HttpUtils;
import com.example.app2.entiy.JavaBean;
import com.example.app2.entiy.TJavaBean;
import com.google.gson.Gson;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.List;
/**
* A simple {@link Fragment} subclass.
*/
public class FirstFragment extends Fragment {
private Button buttonLoad;
private Button buttonReadCard;
private Button buttonDate;
private Handler handler = new Handler();
public FirstFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View inflate = inflater.inflate(R.layout.fragment_first, container, false);
buttonDate = inflate.findViewById(R.id.button_date);
buttonLoad = inflate.findViewById(R.id.button_load);
buttonReadCard = inflate.findViewById(R.id.button_readCard);
getActivity().requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE},1);
firstClick();
SecondClick();
ThirdClick();
return inflate;
}
private void ThirdClick() {
buttonDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar instance = Calendar.getInstance();
new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
Toast.makeText(getContext(), "今天日期:"+year+"/"+month+"/"+dayOfMonth, Toast.LENGTH_SHORT).show();
}
},instance.get(Calendar.YEAR),instance.get(Calendar.MONTH),instance.get(Calendar.DAY_OF_MONTH)).show();
}
});
}
private void SecondClick() {
buttonReadCard.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
File directory = Environment.getExternalStorageDirectory();
FileInputStream fis = null;
try {
fis = new FileInputStream(new File(directory,"one.txt"));
int len = 0;
byte[] b = new byte[1024];
String s = "";
while((fis.read(b)) != -1){
s = new String(b, 0, len);
}
TJavaBean tJavaBean = JSON.parseObject(s, TJavaBean.class);
Log.i("TAG", "run: ->"+s);
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
});
}
private void firstClick() {
buttonLoad.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
new Thread(new Runnable() {
@Override
public void run() {
File directory = Environment.getExternalStorageDirectory();
FileOutputStream fos = null;
try {
fos = new FileOutputStream(new File(directory,"one.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
final String s = HttpUtils.LoadJson("http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=10&page=1");
final FileOutputStream finalFos = fos;
handler.post(new Runnable() {
@Override
public void run() {
try {
finalFos.write(s.getBytes());
finalFos.flush();
finalFos.close();
Log.i("TAG", "run: -> success");
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}).start();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
viewPager-第二个碎片
package com.example.app2.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import com.example.app2.MyAdapter.MyBaseAdapter;
import com.example.app2.R;
import com.example.app2.entiy.JavaBean;
import com.example.app2.entiy.MyAsyncTask;
import java.util.ArrayList;
import java.util.List;
/**
* A simple {@link Fragment} subclass.
*/
public class SecondFragment extends Fragment {
private MyBaseAdapter adapter;
private String path = "http://api.yunzhancn.cn/api/app.interface.php?siteid=78703&act=column&ctype=4";
private List<JavaBean> list = new ArrayList<>();
public SecondFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View inflate = inflater.inflate(R.layout.fragment_second, container, false);
ListView listView = inflate.findViewById(R.id.list_view);
adapter = new MyBaseAdapter(list,getActivity());
listView.setAdapter(adapter);
new MyAsyncTask(list,adapter).execute(path);
return inflate;
}
}
功能类就不写了,异步,适配器,JavaBean
作业三
首先是欢迎页面
布局xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/layout_relative"
tools:context=".WelcomeActivity">
<android.support.v4.view.ViewPager
android:id="@+id/view_page"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
<View
android:id="@+id/vv"
android:layout_centerInParent="true"
android:layout_width="0dp"
android:layout_height="0dp"/>
<TextView
android:layout_marginTop="200dp"
android:layout_below="@id/vv"
android:layout_centerInParent="true"
android:textColor="#000"
android:id="@+id/text_page"
android:textSize="25sp"
android:text="1/3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:visibility="gone"
android:id="@+id/button_now"
android:layout_below="@id/vv"
android:layout_centerInParent="true"
android:layout_marginTop="200dp"
android:textColor="#fff"
android:background="#1921E9"
style="?android:attr/borderlessButtonStyle"
android:text="立即启动"
android:layout_width="150dp"
android:layout_height="wrap_content" />
</RelativeLayout>
其次是欢迎页面的Activity
package com.example.app3;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.example.app3.Fragment.BlankFragment;
import java.util.ArrayList;
import java.util.List;
public class WelcomeActivity extends AppCompatActivity {
private RelativeLayout layoutRelative;
private TextView textPage;
private Button buttonNow;
private ViewPager viewPager;
private int[] icon = {R.mipmap.aa,R.mipmap.bb,R.mipmap.youyou};
private List<Fragment> fragmentsList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
Share.activityList.add(this);
initData();
initView();
}
private void initData() {
for (int i = 0; i < 3; i++) {
BlankFragment blankFragment = new BlankFragment();
fragmentsList.add(blankFragment);
}
}
private void initView() {
layoutRelative = (RelativeLayout) findViewById(R.id.layout_relative);
textPage = (TextView) findViewById(R.id.text_page);
buttonNow = (Button) findViewById(R.id.button_now);
viewPager = findViewById(R.id.view_page);
PagerAdapter();
PagerListener();
}
private void PagerListener() {
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i1) {
}
@Override
public void onPageSelected(int position) {
if(position == 0){
textPage.setText("1/3");
}
if(position == 1){
textPage.setText("2/3");
}
if(position == 2){
textPage.setVisibility(View.GONE);
buttonNow.setVisibility(View.VISIBLE);
buttonNow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
});
}
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
}
private void PagerAdapter() {
viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int i) {
return fragmentsList.get(i);
}
@Override
public int getCount() {
return fragmentsList.size();
}
});
}
}
登录页面
布局xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
tools:context=".MainActivity">
<LinearLayout
android:layout_centerInParent="true"
android:layout_width="350dp"
android:orientation="vertical"
android:layout_height="wrap_content">
<EditText
android:background="@drawable/edit_background"
android:id="@+id/edit_user"
android:paddingLeft="10dp"
android:hint="请输入用户名"
android:layout_width="300dp"
android:layout_height="45dp" />
<EditText
android:background="@drawable/edit_background"
android:layout_marginTop="20dp"
android:id="@+id/edit_pwd"
android:paddingLeft="10dp"
android:hint="请输入密码"
android:layout_width="300dp"
android:layout_height="45dp" />
<CheckBox
android:layout_marginTop="10dp"
android:id="@+id/check_heat"
android:text="记住用户名"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button_login"
android:layout_gravity="center"
android:background="#636363"
android:textColor="#fff"
android:text="登录"
style="?android:attr/borderlessButtonStyle"
android:layout_width="200dp"
android:layout_height="30dp" />
</LinearLayout>
</RelativeLayout>
LoginActivity
package com.example.app3;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.example.app3.JavaBean.MyJavaBean;
import com.google.gson.Gson;
public class MainActivity extends AppCompatActivity {
private EditText editUser;
private EditText editPwd;
private CheckBox userHeat;
private Button buttonLogin;
private long exitTime = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Share.activityList.add(this);
initView();
}
private void initView() {
editUser = (EditText) findViewById(R.id.edit_user);
editPwd = (EditText) findViewById(R.id.edit_pwd);
userHeat = (CheckBox) findViewById(R.id.check_heat);
buttonLogin = (Button) findViewById(R.id.button_login);
loginListener();
}
private void loginListener() {
SharedPreferences user = getSharedPreferences("user", MODE_PRIVATE);
boolean is = user.getBoolean("is", false);
if(is){
String pwd = user.getString("pwd", "");
String users = user.getString("user", "");
editPwd.setText(pwd);
editUser.setText(users);
userHeat.setChecked(true);
}
buttonLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String user = editUser.getText().toString();
String pwd = editPwd.getText().toString();
EditListener(user,pwd);
CheckListener(user,pwd);
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
}
});
}
private void CheckListener(String user, String pwd) {
if(userHeat.isChecked()){
SharedPreferences.Editor edit = getSharedPreferences("user", MODE_PRIVATE).edit();
Gson gson = new Gson();
edit.putString("user",user);
edit.putString("pwd",pwd);
edit.putBoolean("is",true);
edit.commit();
}else{
SharedPreferences sp = getSharedPreferences("user", MODE_PRIVATE);
SharedPreferences.Editor edit = sp.edit();
edit.clear();
edit.commit();
}
}
private void EditListener(String user, String pwd) {
if(user == null || user.equals("")){
Toast.makeText(MainActivity.this, "用户名不能为空", Toast.LENGTH_SHORT).show();
return;
}
if(pwd == null || pwd.equals("")){
Toast.makeText(MainActivity.this, "密码不能为空", Toast.LENGTH_SHORT).show();
return;
}
if(pwd.length() < 6 || pwd.length() > 15){
Toast.makeText(MainActivity.this, "密码长度不服", Toast.LENGTH_SHORT).show();
return;
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK){
if (System.currentTimeMillis() - exitTime > 2000) {
Share.FinishActivity();
return true;
}
}
return super.onKeyDown(keyCode, event);
}
}
之后是我们的轮播主要页面
布局xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main2Activity">
<LinearLayout
android:orientation="horizontal"
android:id="@+id/lay"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_weight="8"
android:layout_width="0dp"
android:layout_height="50dp">
</android.support.design.widget.TabLayout>
<Button
android:textSize="20sp"
android:text="+"
style="?android:attr/borderlessButtonStyle"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="wrap_content" />
</LinearLayout>
<android.support.v4.view.ViewPager
android:layout_below="@id/lay"
android:layout_above="@id/lay_c"
android:id="@+id/view_pageo"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<LinearLayout
android:id="@+id/lay_c"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="50dp">
<RadioGroup
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioButton
android:text="1"
android:gravity="center"
android:button="@null"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<RadioButton
android:text="2"
android:gravity="center"
android:button="@null"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<RadioButton
android:id="@+id/radio_select"
android:text="自选"
android:gravity="center"
android:button="@null"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<RadioButton
android:text="4"
android:gravity="center"
android:button="@null"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<RadioButton
android:id="@+id/radio_mine"
android:text="我的"
android:gravity="center"
android:button="@null"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
</RadioGroup>
</LinearLayout>
</RelativeLayout>
Main2Activity中的
package com.example.app3;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.Toast;
import com.example.app3.Fragment.FirstFragment;
import com.example.app3.Fragment.FiveFragment;
import com.example.app3.Fragment.FourFragment;
import com.example.app3.Fragment.SecondFragment;
import com.example.app3.Fragment.ThirdFragment;
import com.example.app3.FunnyAdapter.MyAdapter;
import com.example.app3.JavaBean.FoodJavaBean;
import com.example.app3.enity.MyAsyncTask;
import java.security.Key;
import java.util.ArrayList;
import java.util.List;
public class Main2Activity extends AppCompatActivity {
private List<FoodJavaBean.DataBean> list = new ArrayList<>();
private List<String> dataList = new ArrayList<>();
private List<Fragment> fragmentList = new ArrayList<>();
private List<String> StringList = new ArrayList<>();
private MyAdapter adapter;
private TabLayout tabLayout;
private ViewPager viewPager;
private FirstFragment firstFragment;
private RadioButton radio_select,radio_mine;
private long exitTime = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
initData();
initView();
Share.activityList.add(this);
}
private void initData() {
firstFragment = new FirstFragment();
Bundle bundle = new Bundle();
bundle.putBoolean("isCheck",false);
firstFragment.setArguments(bundle);
fragmentList.add(firstFragment);
fragmentList.add(new SecondFragment());
fragmentList.add(new ThirdFragment());
fragmentList.add(new FourFragment());
fragmentList.add(new FiveFragment());
dataList.add("头条");
dataList.add("7×24小时");
dataList.add("股票");
dataList.add("日历");
dataList.add("美股");
}
private void initView() {
viewPager = findViewById(R.id.view_pageo);
tabLayout = findViewById(R.id.tab_layout);
radio_mine = findViewById(R.id.radio_mine);
radio_select = findViewById(R.id.radio_select);
adapter = new MyAdapter(list,this);
PagerAdapter();
TabListener();
PagerListener();
RadioListener();
}
private void RadioListener() {
radio_select.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
viewPager.setCurrentItem(2);
}
});
radio_mine.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
viewPager.setCurrentItem(4);
}
});
}
private void PagerListener() {
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i1) {
}
@Override
public void onPageSelected(int i) {
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
}
private void TabListener() {
tabLayout.setupWithViewPager(viewPager);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
switch (tab.getPosition()){
case 0:
Bundle bundle = new Bundle();
bundle.putBoolean("isCheck",true);
firstFragment.setArguments(bundle);
// Share.isCheck = true;
break;
}
}
});
}
private void PagerAdapter() {
viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int i) {
return fragmentList.get(i);
}
@Override
public int getCount() {
return fragmentList.size();
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return dataList.get(position);
}
});
}
}
接下来是碎片
碎片一
package com.example.app3.Fragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import com.example.app3.FunnyAdapter.MyAdapter;
import com.example.app3.JavaBean.FoodJavaBean;
import com.example.app3.R;
import com.example.app3.Share;
import com.example.app3.enity.MyAsyncTask;
import java.util.ArrayList;
import java.util.List;
/**
* A simple {@link Fragment} subclass.
*/
public class FirstFragment extends Fragment {
private Button buttonGetJson;
private Button buttonShow;
private ListView listView;
private String path = "http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=3";
private List<FoodJavaBean.DataBean> list = new ArrayList<>();
private MyAdapter adapter;
public FirstFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View inflate = inflater.inflate(R.layout.fragment_first, container, false);
buttonGetJson = inflate.findViewById(R.id.button_getJson);
buttonShow = inflate.findViewById(R.id.button_show);
listView = inflate.findViewById(R.id.list_view);
adapter = new MyAdapter(list,getActivity());
// Bundle b = getArguments();
// boolean isCheck = b.getBoolean("isCheck");
// if(isCheck){
// buttonGetJson.setVisibility(View.VISIBLE);
// buttonShow.setVisibility(View.VISIBLE);
// }
buttonGetJson.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "下载已完成!", Toast.LENGTH_SHORT).show();
new MyAsyncTask(list,adapter,getActivity()).execute(path);
}
});
buttonShow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listView.setAdapter(adapter);
}
});
return inflate;
}
}
碎片二
package com.example.app3.Fragment;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import com.example.app3.R;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
/**
* A simple {@link Fragment} subclass.
*/
public class ThirdFragment extends Fragment {
private Button button_show;
private TextView textView_show;
private Handler handler = new Handler();
public ThirdFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View inflate = inflater.inflate(R.layout.fragment_third, container, false);
button_show = inflate.findViewById(R.id.button_showJson);
textView_show = inflate.findViewById(R.id.text_json);
button_show.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
File directory = Environment.getExternalStorageDirectory();
FileInputStream fis = null;
try {
fis = new FileInputStream(new File(directory,"newJson.txt"));
int len = 0;
byte[] b = new byte[1024];
String s = "";
while((len = fis.read(b)) != -1){
s = new String(b, 0, len);
}
final String finalS = s;
handler.post(new Runnable() {
@Override
public void run() {
textView_show.setText(finalS);
}
});
Log.i("TAG", "run: ->"+s);
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
});
return inflate;
}
}
碎片三
package com.example.app3.Fragment;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import com.example.app3.Main2Activity;
import com.example.app3.MainActivity;
import com.example.app3.R;
/**
* A simple {@link Fragment} subclass.
*/
public class FiveFragment extends Fragment {
private Button button_exit;
public FiveFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View inflate = inflater.inflate(R.layout.fragment_five, container, false);
button_exit = inflate.findViewById(R.id.button_exit);
button_exit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences user = getActivity().getSharedPreferences("user", Context.MODE_PRIVATE);
SharedPreferences.Editor edit = user.edit();
edit.clear();
edit.commit();
Toast.makeText(getActivity(), "用户删除成功", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getActivity(), MainActivity.class);
startActivity(intent);
}
});
return inflate;
}
}
总结,这次第四个技能有2个地放未实现,一、按钮隐藏显示,二、序列化Gson