我是 Android 编程新手。我正在从书中做这个项目。我编写了类来返回名称和地址,然后为它编写了 OnClickListener,但是当我单击保存按钮时它似乎不起作用。这是我的两个 Java 文件:
package com.example.lunchlist;
public class Returant {
private String name="";
private String address="";
public String getName() {
return(name);
}
public void setName(String name) {
this.name=name;
}
public String getAddress() {
return(address);
}
public void setAddress(String address) {
this.address=address;
}
}
这是我的 MainActivity 文件:
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
Returant r = new Returant();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button save = (Button) findViewById(R.id.save);
save.setOnClickListener(onSave);
}
private View.OnClickListener onSave=new View.OnClickListener() {
public void onClick(View v) {
EditText name=(EditText)findViewById(R.id.name);
EditText address=(EditText)findViewById(R.id.addr);
r.setName(name.getText().toString());
r.setAddress(address.getText().toString());
}
};
}