团队开发 --Android

今天是第一次写,但不是第一天开始,放五一小长假,我的队友回家了,我们两个就属于分工合作,反正就最近几天的成果。

 

今天不早了,其他注意事项明天说,今天先上代码。

MainActivity
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.FragmentManager;

import com.example.myapplication3.R;
import com.example.myapplication3.activity.bean.User;
import com.example.myapplication3.activity.dao.Dao;
import com.example.myapplication3.activity.fragment.FiratFragment;
import com.example.myapplication3.activity.mysql.getConnection;

public class MainActivity extends AppCompatActivity {
    Button register = null;
    EditText name = null;
    EditText password = null;
    User user = new User();
    Dao dao = new Dao();
String ne = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 获取碎片管理器
//        FragmentManager fm = getSupportFragmentManager();
        // 在主容器里添加
//        fm.beginTransaction().replace(R.id.fragment_firat,new FiratFragment()).commit();

        findALl();
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                user = new User(name.getText().toString(),password.getText().toString());
                Thread thread = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        while (dao.checkUser(user))
                        {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast toast = Toast.makeText(MainActivity.this,"登录成功!",Toast.LENGTH_SHORT);
                                    toast.show();
                                    Intent intent = new Intent(MainActivity.this, MainActivity2.class);
                                    Bundle bundle = new Bundle();
                                    bundle.putString("username",name.getText().toString());
                                    intent.putExtras(bundle);
                                    startActivity(intent);
                                }
                            });

                            break;
                        }
                        while(dao.checkUser(user) == false)
                        {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast toast = Toast.makeText(MainActivity.this,"登录失败!",Toast.LENGTH_SHORT);
                                    toast.show();
                                }
                            });
                            break;
                        }


                    }
                });thread.start();

            }
        });
    }

    private void findALl()
    {
        register = findViewById(R.id.register);
        name = findViewById(R.id.name);
        password = findViewById(R.id.password);
    }

}

 

MainActivity2
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.viewpager2.widget.ViewPager2;

import com.example.myapplication3.R;
import com.example.myapplication3.activity.fragmentpageradapter.myFragmentPagerAdapter;
import com.example.myapplication3.activity.fragment.Fragment1;

public class MainActivity2 extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
    private String name;
    private RadioButton a;
    private RadioButton b;
    private RadioButton c;
    private RadioButton d;
    private RadioGroup rg_tab_bar;
    private ViewPager2 viewpage;
    private myFragmentPagerAdapter myFragmentPagerAdapter;
    public static final int PAGE_ONE = 0;
    public static final int PAGE_TWO = 1;
    public static final int PAGE_THREE = 2;
    public static final int PAGE_FOUR = 3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        bindView();
        a.setChecked(true);
    }

    private void bindView()
    {
        Bundle bundle = getIntent().getExtras();
        if(bundle != null)
        {
            name = bundle.getString("username");
        }
        a = (RadioButton) findViewById(R.id.a);
        b = (RadioButton) findViewById(R.id.b);
        c = (RadioButton) findViewById(R.id.b);
        d = (RadioButton) findViewById(R.id.d);
        rg_tab_bar = (RadioGroup) findViewById(R.id.rg_tab_bar);
        viewpage = (ViewPager2) findViewById(R.id.viewpager);

        rg_tab_bar.setOnCheckedChangeListener(this);
        myFragmentPagerAdapter = new myFragmentPagerAdapter(getSupportFragmentManager(),getLifecycle());
        viewpage.setAdapter(myFragmentPagerAdapter);
        viewpage.setCurrentItem(0);
        viewpage.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                switch (position) {
                    case PAGE_ONE:
                        if (myFragmentPagerAdapter.createFragment(PAGE_ONE) != null)
                        {
                            Fragment1 fragment1 = (Fragment1) myFragmentPagerAdapter.createFragment(PAGE_ONE);
                            fragment1.upData(name);
                        }
                        rg_tab_bar.check(R.id.a);
                        break;
                    case PAGE_TWO:
                        rg_tab_bar.check(R.id.b);
                        break;
                    case PAGE_THREE:
                        rg_tab_bar.check(R.id.c);
                        break;
                    case PAGE_FOUR:
                        rg_tab_bar.check(R.id.d);
                        break;
                }
            }
            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
                if (state == 2) {
                    switch (viewpage.getCurrentItem()) {
                        case PAGE_ONE:
                            a.setChecked(true);
                            break;
                        case PAGE_TWO:
                            b.setChecked(true);
                            break;
                        case PAGE_THREE:
                            c.setChecked(true);
                            break;
                        case PAGE_FOUR:
                            d.setChecked(true);
                            break;
                    }
                }
            }


        });


    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.a:
                viewpage.setCurrentItem(PAGE_ONE);
                break;
            case R.id.b:
                viewpage.setCurrentItem(PAGE_TWO);
                break;
            case R.id.c:
                viewpage.setCurrentItem(PAGE_THREE);
                break;
            case R.id.d:
                viewpage.setCurrentItem(PAGE_FOUR);
                break;
        }
    }
}

Dao

import com.example.myapplication3.activity.bean.State;
import com.example.myapplication3.activity.bean.User;
import com.example.myapplication3.activity.mysql.getConnection;

import java.sql.*;


public class Dao {

    Connection connection ;

    //    添加信息
    public void insertOld(User user)
    {
        connection = getConnection.getcoon();
        String sql = "insert into information (`name`,`password`) value (?,?)";

        try {
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, user.getName());
            ps.setString(2, user.getPassword());
            ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public void insertSubmit(State st)
    {
        connection = getConnection.getcoon();
        String sql = "insert into submit (`name`,`state`) value (?,?)";
        try {
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,st.getName());
            ps.setString(2,st.getState());
            ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    //    查询用户是否存在
    public boolean checkUser(User user)
    {
        PreparedStatement ps = null;
        ResultSet re = null;
        connection = getConnection.getcoon();
        String sql = "select * from information where name = ? and password = ? ";
        try {
            ps = connection.prepareStatement(sql);
            ps.setString(1, user.getName());
            ps.setString(2, user.getPassword());
            re = ps.executeQuery();
            return re.next();
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            try { if (re != null) re.close(); } catch (Exception e) { e.printStackTrace(); }
            try { if (ps != null) ps.close(); } catch (Exception e) { e.printStackTrace(); }
            try { if (connection != null) connection.close(); } catch (Exception e) { e.printStackTrace(); }
        }
        return false;
    }
}

User

public class User {
    private String name;
    private String password;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String  getPassword() {
        return password;
    }

    public void setPassword(String  password) {
        this.password = password;
    }

    public User(String name, String password) {
        this.name = name;
        this.password = password;
    }

    public User() {
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".activity.activity.MainActivity">

    <FrameLayout
        android:id="@+id/fragment_firat"
        android:name="com.example.myapplication3.activity.fragment.FiratFragment"
        android:layout_width="match_parent"
        android:layout_height="70dp"/>
    <!-- 姓名   -->
    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:columnCount="2">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="姓 名:"
            android:textSize="20sp"/>


        <EditText
            android:id="@+id/name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="text" />

    </GridLayout>

    <!--密码-->

    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:columnCount="2">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密 码:"
            android:textSize="20sp"/>


        <EditText
            android:id="@+id/password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPassword" />

    </GridLayout>

    <Button
        android:id="@+id/register"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登录" />

</LinearLayout>

activity_main2.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:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".activity.activity.MainActivity2"> <RelativeLayout android:id="@+id/ly_top_bar" android:layout_width="match_parent" android:layout_height="48dp"> <TextView android:id="@+id/txt_topbar" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerInParent="true" android:gravity="center" android:text="信息" android:textSize="18sp" /> <View android:layout_width="match_parent" android:layout_height="2px" android:layout_alignParentBottom="true"/> </RelativeLayout> <RadioGroup android:id="@+id/rg_tab_bar" android:layout_width="match_parent" android:layout_height="56dp" android:layout_alignParentBottom="true" android:background="@color/white" android:orientation="horizontal"> <RadioButton android:id="@+id/a" style="@style/tab_menu_item" android:text="one"/> <RadioButton android:id="@+id/b" style="@style/tab_menu_item" android:text="two"/> <RadioButton android:id="@+id/c" style="@style/tab_menu_item" android:text="there"/> <RadioButton android:id="@+id/d" style="@style/tab_menu_item" android:text="four"/> </RadioGroup> <androidx.viewpager2.widget.ViewPager2 android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@id/rg_tab_bar" android:layout_below="@id/ly_top_bar"/> </RelativeLayout>

 

 

posted @ 2024-05-06 00:04  *太¥^白%  阅读(8)  评论(0编辑  收藏  举报