7.28

menu2代码

package com.example.month7day26ap;

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.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import java.util.List;

public class Menu2 extends AppCompatActivity {

private TabLayout menu2TabLayout;
private RadioGroup menu2RadioGroup;
private RadioButton menu2RadioButtonRecommend;
private RadioButton menu2RadioButtonPhone;
private RadioButton menu2RadioButtonAuto;
private RadioButton menu2RadioButtonComputer;
private RadioButton menu2RadioButtonClassify;
private ViewPager menu2ViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_menu2);

    menu2TabLayout = (TabLayout) findViewById(R.id.menu2_tabLayout);
    menu2RadioGroup = (RadioGroup) findViewById(R.id.menu2_radioGroup);
    menu2RadioButtonRecommend = (RadioButton) findViewById(R.id.menu2_radioButton_recommend);
    menu2RadioButtonPhone = (RadioButton) findViewById(R.id.menu2_radioButton_phone);
    menu2RadioButtonAuto = (RadioButton) findViewById(R.id.menu2_radioButton_auto);
    menu2RadioButtonComputer = (RadioButton) findViewById(R.id.menu2_radioButton_computer);
    menu2RadioButtonClassify = (RadioButton) findViewById(R.id.menu2_radioButton_classify);
    menu2ViewPager = (ViewPager) findViewById(R.id.menu2_viewPager);

// menu2TabLayout.addTab();
}
class Find_tab_Adapter extends FragmentPagerAdapter{
private List fragments;//存放fragment
private List list_title;//文字

    public Find_tab_Adapter(FragmentManager fm, List fragments, List list_title) {
        super(fm);
        this.fragments = fragments;
        this.list_title = list_title;
    }

    public Find_tab_Adapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        return fragments.get(i);
    }

    @Override
    public int getCount() {
        return list_title.size();
    }

    /**
     * 显示tab上的名字
     * @param position
     * @return
     */
    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return list_title.get(position%list_title.size());
    }
}

}

主界面代码

package com.example.month7day26ap;

import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.UserHandle;
import android.preference.PreferenceManager;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.month7day26ap.userData.User;
import com.example.month7day26ap.userData.UserHelper;
import com.example.month7day26ap.userData.UserUtil;

public class Menu extends AppCompatActivity {
private EditText menuEditName;
private EditText menuEditPwd;
private Button menuButtonEnter;
private CheckBox checkBox;
private TextView tv;
//内存
private SharedPreferences sharedPreferences;
private SharedPreferences.Editor edit;
private UserHelper uh;
private SQLiteDatabase db;
private UserUtil userUtil;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_menu);

    menuEditName = (EditText) findViewById(R.id.menu_edit_name);
    menuEditPwd = (EditText) findViewById(R.id.menu_edit_pwd);
    menuButtonEnter = (Button) findViewById(R.id.menu_button_enter);
    tv=findViewById(R.id.Menu_text_LogIn);
    checkBox=findViewById(R.id.menu_checkBox);
    uh=new UserHelper(this,"user.db",null,1);
    db=uh.getReadableDatabase();

    userUtil = new UserUtil(db);
    menuButtonEnter.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String name = menuEditName.getText().toString();
            String pwd = menuEditPwd.getText().toString();
            if( name.equals("admin")&&pwd.equals("admin")){
                if( checkBox.isChecked()){

                    String query = userUtil.query(name);
                    if( query==null){
                        User user = new User();
                        user.setName("admin");
                        user.setPwd("admin");
                        userUtil.insert(user);
                        Toast.makeText(Menu.this, "登录失败,新建用户---》"+user.toString()+"\n请在三秒内记住信息。", Toast.LENGTH_LONG).show();
                    }else{
                        Toast.makeText(Menu.this, query, Toast.LENGTH_SHORT).show();
                    }

// sharedPreferences=PreferenceManager.getDefaultSharedPreferences(Menu.this);
// edit = sharedPreferences.edit();
// edit.putBoolean(“key”,true);
// //写入内存。
// edit.apply();
}
Toast.makeText(Menu.this, “登陆成功。”, Toast.LENGTH_SHORT).show();
}
}
});

    tv.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final AlertDialog.Builder builder = new AlertDialog.Builder(Menu.this);
            final View inflate = LayoutInflater.from(Menu.this).inflate(R.layout.enter_layout, null);
            builder.setView(inflate);
            builder.setTitle("登录界面");
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    EditText id = inflate.findViewById(R.id.enter_editText_id);
                    String name = id.getText().toString();
                    EditText epwd = inflate.findViewById(R.id.enter_editText_pwd);
                    String pwd = epwd.getText().toString();
                    User user = new User();
                    user.setPwd(pwd);
                    user.setName(name);
                    boolean enter = userUtil.enter(user);
                    if( enter){
                        Toast.makeText(Menu.this, "登陆成功。", Toast.LENGTH_SHORT).show();
                        
                    }else{
                        Toast.makeText(Menu.this, "登录失败。", Toast.LENGTH_SHORT).show();
                    }
                }
            });
            AlertDialog alertDialog = builder.create();
            alertDialog.show();
        }
    });

}

}

fragmentxml



### 轮播图 package com.example.month7day26ap;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
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.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.month7day26ap.GuidePager.GuideFragment;
import com.example.month7day26ap.userData.UserHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {
private static final String TAG = “132321”;
private ViewPager vp;
private int[] arr={R.drawable.image1,R.drawable.image2,R.drawable.image3,
R.drawable.image4,R.drawable.image5};
private List list=new ArrayList<>();
private int flag=0;
private UserHelper uh;

@SuppressLint("HandlerLeak")
private Handler han=new Handler(){
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        int arg1 = msg.arg1;
        if( arg1==4){
            Toast.makeText(MainActivity.this, "点击进入下个页面。", Toast.LENGTH_SHORT).show();
        }
        vp.setCurrentItem(arg1);
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    vp=findViewById(R.id.GuidePager_viewPager);


    for (int i = 0; i < 5; i++) {
        GuideFragment guideFragment = new GuideFragment();
        guideFragment.setImage(arr[i]);
        list.add(guideFragment);
    }

    vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
        @Override
        public Fragment getItem(int i) {
            return list.get(i);
        }

        @Override
        public int getCount() {
            return list.size();
        }
    });

    final Timer timer = new Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            Message obtain = Message.obtain();
            obtain.arg1=flag;
            han.sendMessage(obtain);
            flag++;
            if( flag==5){
                timer.cancel();
            }
        }
    },0,1500);

    vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int i, float v, int i1) {
            Log.i(TAG, "onPageScrolled: i\t"+i);
            Log.i(TAG, "onPageScrolled: i10\t"+i1);
        }

        @Override
        public void onPageSelected(int i) {
            Log.i(TAG, "onPageSelected: "+i);
            if (i==list.size()-1){
                GuideFragment fragment = list.get(i);
                ImageView iv = fragment.getIv();
                iv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(MainActivity.this, "跳转完毕。", Toast.LENGTH_SHORT).show();

                        Intent it = new Intent(MainActivity.this, Menu.class);
                        startActivity(it);
                    }
                });
            }
        }

        @Override
        public void onPageScrollStateChanged(int i) {

        }
    });

// vp.addOnPageChangeListener(new On);

}

}

你可能感兴趣的:(安卓第二个月)