为什么Save Button在我的Sample Android App中不起作用

时间:2015-10-11 06:43:06

标签: java android onclicklistener

我是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>

现在,当我点击“保存”按钮时,没有任何反应.. 所以有人可以帮助我摆脱这种情况。

提前致谢。

3 个答案:

答案 0 :(得分:3)

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;
    }
}

答案 1 :(得分:1)

试试这段代码:

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);

答案 2 :(得分:0)

哦,感谢大家给我时间问题并建议我去解决这个问题。 但在审查了所有代码行后,我发现了自己的错误。

该错误是在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>

再次感谢所有人。