繁体   English   中英

为什么“保存”按钮在我的示例Android应用中不起作用

[英]Why Save Button doesn't work in my Sample Android App

我是Android系统的初学者,正尝试从中创建一个简单的注册。 在此应用程序中,我创建了所有需要的代码,但单击“保存”按钮后未给出任何响应。

这是注册表格的代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:id="@+id/txtViewName"
        android:text="Student's Name :"
        android:layout_marginTop="30dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:id="@+id/txtViewFather"
        android:text="Father's Name :"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/txtViewName"
        android:layout_marginTop="29dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:id="@+id/txtViewMother"
        android:text="Mother's Name :"
        android:layout_below="@+id/txtViewFather"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="29dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:id="@+id/txtViewAge"
        android:text="Age :"
        android:layout_below="@+id/txtViewMother"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="29dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:id="@+id/txtViewClass"
        android:text="Class :"
        android:layout_below="@id/txtViewAge"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="29dp" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:id="@+id/txtEditName"
        android:ems="10"
        android:layout_toRightOf="@+id/txtViewName"
        android:layout_above="@+id/txtViewFather"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:id="@+id/txtEditFather"
        android:ems="10"
        android:layout_toRightOf="@+id/txtViewFather"
        android:layout_above="@+id/txtViewMother"
        android:layout_alignParentRight="true"
        android:layout_alignLeft="@+id/txtEditName"
        android:layout_alignStart="@+id/txtEditName"
        android:layout_alignRight="@+id/txtEditName"
        android:layout_alignEnd="@+id/txtEditName" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:id="@+id/txtEditMother"
        android:ems="10"
        android:layout_above="@+id/txtViewAge"
        android:layout_toRightOf="@+id/txtViewMother"
        android:layout_alignLeft="@+id/txtEditName"
        android:layout_alignStart="@+id/txtEditName"
        android:layout_alignRight="@+id/txtEditName"
        android:layout_alignEnd="@+id/txtEditName" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:id="@+id/txtEditAge"
        android:ems="10"
        android:layout_above="@+id/txtViewClass"
        android:layout_toRightOf="@+id/txtViewAge"
        android:layout_alignLeft="@+id/txtEditName"
        android:layout_alignStart="@+id/txtEditName"
        android:layout_alignRight="@+id/txtEditName"
        android:layout_alignEnd="@+id/txtEditName" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/txtEditClass"
        android:layout_alignBottom="@id/txtViewClass"
        android:layout_alignLeft="@+id/txtEditName"
        android:layout_alignStart="@+id/txtEditName"
        android:layout_alignRight="@+id/txtEditName"
        android:layout_alignEnd="@+id/txtEditName" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btnBack"
        android:text="Back"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btnSave"
        android:text="Save"
        android:layout_toLeftOf="@+id/btnBack"
        android:layout_alignParentBottom="true" />

</RelativeLayout>

这是StudentDetail.java类

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import android.view.View.OnClickListener;

import java.util.ArrayList; 

public class StudentDetail extends AppCompatActivity implements android.view.View.OnClickListener {

Button btnSave;
Button btnBack;

EditText txtEditName;
EditText txtEditFather;
EditText txtEditMother;
EditText txtEditAge;
EditText txtEditClass;

private int _student_id = 0;

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

    btnSave = (Button) findViewById(R.id.btnSave);
    btnBack = (Button) findViewById(R.id.btnBack);

    txtEditName = (EditText) findViewById(R.id.txtEditName);
    txtEditFather = (EditText) findViewById(R.id.txtEditFather);
    txtEditMother = (EditText) findViewById(R.id.txtEditMother);
    txtEditAge = (EditText) findViewById(R.id.txtEditAge);
    txtEditClass= (EditText) findViewById(R.id.txtEditClass);

    btnSave.setOnClickListener(this);
    btnBack.setOnClickListener(this);

    _student_id = 0;
    Intent intent = getIntent();
    _student_id = intent.getIntExtra("student_Id", 0);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onClick(View view) {

    if (view == findViewById(R.id.btnSave)){

        StudentRepo repo = new StudentRepo(this);
        Student student = new Student();
        student.name = txtEditName.getText().toString();
        student.father_name = txtEditFather.getText().toString();
        student.mother_name = txtEditMother.getText().toString();
        student.age = Integer.parseInt(txtEditAge.getText().toString());
        student.student_class = txtEditClass.getText().toString();
        student.student_id =_student_id;

        if (_student_id==0){
            _student_id = repo.insert(student);

            Toast.makeText(this,"New Student Insert",Toast.LENGTH_SHORT).show();
        }
    } else if(view== findViewById(R.id.btnBack)){
        finish();
    }


}

}

这是AndroidManifest.xml代码

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.studentregistration" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.studentregistration.StudentDetail"
            android:label="@string/title_activity_entry_form" >
        </activity>
    </application>

</manifest>

现在,当我单击“保存”按钮时,什么也没有发生。.因此有人可以帮助我解决这种情况。

提前致谢。

onClick函数中,在视图ID上使用switch-case ,如下所示:

@override
public void onClick(View view) {
    switch(view.getId()) {
        case R.id.btnSave:
            // Your custom save code goes here ...
            break;
        case R.id.btnBack:
            // ...
            break;
    }
}

试试这个代码:

btnSave.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View view) {
            StudentRepo repo = new StudentRepo(StudentDetail .this);
            Student student = new Student();
            student.name = txtEditName.getText().toString();
            student.father_name = txtEditFather.getText().toString();
            student.mother_name = txtEditMother.getText().toString();
            student.age = Integer.parseInt(txtEditAge.getText().toString());
            student.student_class = txtEditClass.getText().toString();
            student.student_id =_student_id;

            if (_student_id==0){
                _student_id = repo.insert(student);

                Toast.makeText(this,"New Student Insert",Toast.LENGTH_SHORT).show();
            }
   }
});

btnBack.setOnClickListener(new View.OnClickListener() {

   @Override
   public void onClick(View view) {
         finish();
   }
});

代替

btnSave.setOnClickListener(this);
btnBack.setOnClickListener(this);

谢谢大家,给我一些时间,建议我做些修正错误的工作。 但是,在逐行回顾所有代码之后,我发现了这个错误。

该错误是在AndroidManifest.xml文件中启动了错误的活动。 为了解决这个问题,我从MainActivity标记中删除了该标记,并将其放置到StudentDetail活动标记中,一切开始正常运行。

更新的代码是:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.studentregistration" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
        </activity>
        <activity
            android:name="com.example.studentregistration.StudentDetail"
            android:label="@string/title_activity_entry_form" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

再次感谢所有人。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM