Grundlegende Schülerinformationen für Android Studio (Parameterübertragung)

Grundlegende Schülerinformationen für Android Studio (Parameterübertragung)

Überblick

Entwerfen Sie eine APP, um die Realisierung von Funktionen abzuschließen, mit denen Namen, Hauptfächer, Alter und Punktzahlen der Schüler hinzugefügt werden können. Ich werde hier nicht näher auf den Prozess der Projekterstellung usw. eingehen und mich hauptsächlich auf Bilder und Code-Erklärungen stützen. Hier ist die Netzwerkdiskettenadresse des Projektcodes für alle. Wenn Sie sie benötigen, können Sie sie selbst herunterladen.
https://pan.baidu.com/s/1qJ84gxQ44oj08g6TrYKorA .
Extraktionscode: ouun

1. Schnittstellenlayout

Datei activity_main_xml

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="学生信息"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.309"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.28" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="姓名"
        android:id="@+id/name_edit"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="专业"
        android:id="@+id/major_edit"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="年龄"
        android:inputType="number"
        android:id="@+id/age_edit"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="成绩"
        android:inputType="numberDecimal"
        android:id="@+id/score_edit"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="发送信息"
            android:id="@+id/btn_save"
            android:layout_weight="1"/>
        <Button
            android:id="@+id/btn_submit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="添加信息"
            android:layout_weight="1"/>
    </LinearLayout>

info_xml Datei

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textSize="25sp"
        android:textColor="@color/teal_200"
        android:id="@+id/infoshow"/>

Effektbild
Fügen Sie hier eine Bildbeschreibung ein

Zweitens der Hauptcode

mainactivity.java

public class MainActivity extends AppCompatActivity {
    
    
    Button btn_submit;
    Button btn_save;
    EditText name_edit;
    EditText major_edit;
    EditText age_edit;
    EditText score_edit;
    ArrayList<student> students=new ArrayList<student>();
    @Override
    protected  void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        name_edit=(EditText) findViewById(R.id.name_edit);
        major_edit=(EditText) findViewById(R.id.major_edit);
        age_edit=(EditText) findViewById(R.id.age_edit);
        score_edit=(EditText) findViewById(R.id.score_edit);
        btn_save=(Button) findViewById(R.id.btn_save);
        btn_save.setOnClickListener(new View.OnClickListener() {
    
    
            @Override
            public void onClick(View v) {
    
    
                String name=name_edit.getText().toString();
                String major=major_edit.getText().toString();
                int age=Integer.parseInt(age_edit.getText().toString());
                double score=Double.parseDouble(score_edit.getText().toString());
                student s=new student(name,major,age,score);
                students.add(s);
                name_edit.setText(" ");
                major_edit.setText(" ");
                age_edit.setText(" ");
                score_edit.setText(" ");
            }
        });
        btn_submit=(Button) findViewById(R.id.btn_submit);
        btn_submit.setOnClickListener(new View.OnClickListener()  {
    
    
            @Override
            public void onClick(View v) {
    
    
                Intent intent=new Intent();
                intent.setClass(MainActivity.this,info.class);
                intent.putExtra("students",students);
                startActivity(intent);
            }
        });
    }
}

student.java

public class student implements Serializable{
    
    
    private String name;
    private String major;
    private int  age;
    private double score;
    student(String name,String major,int age,double score){
    
    
        this.name=name;
        this.major=major;
        this.age=age;
        this.score=score;
    }

    public String getName() {
    
    
        return name;
    }

    public void setName(String name) {
    
    

        this.name = name;
    }

    public String getMajor() {
    
    
        return major;
    }

    public void setMajor(String major) {
    
    
        this.major = major;
    }

    public int getAge() {
    
    

        return age;
    }

    public void setAge(int age) {
    
    
        this.age = age;
    }

    public double getScore() {
    
    
        return score;
    }

    public void setScore(double score) {
    
    
        this.score = score;
    }
}

info.java

public class info extends AppCompatActivity {
    
    
    TextView infoshow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_info);
        infoshow=(TextView) findViewById(R.id.infoshow);
        Bundle bundle=getIntent().getExtras();
        ArrayList<student> students=( ArrayList<student>) bundle.get("students");
        for(student s:students){
    
    
            infoshow.append("姓名:"+s.getName()+";专业:"+s.getMajor()+";年龄:"+s.getAge()+";成绩:"+s.getScore()+"\n");
        }
    }
}

Drei, Effektdemonstration

Informationen hinzufügen
Fügen Sie hier eine Bildbeschreibung ein
Klicken Sie hier, um Informationen zu senden. Die Informationsleiste wird gelöscht.
Fügen Sie hier eine Bildbeschreibung ein
Klicken Sie auf Informationen hinzufügen. Sie können die zuvor hinzugefügten Informationen anzeigen
Fügen Sie hier eine Bildbeschreibung ein

Ich denke du magst

Origin blog.csdn.net/QWERTYzxw/article/details/115186728
Empfohlen
Rangfolge