创建访问 oncreate() 对象的用户定义方法
create user defined methods that access objects of oncreate()
我正在尝试开发一个应用程序,其中我有一组代码,每次按下 5 个按钮中的一个按钮时都必须调用这些代码。只有与之关联的要打开的文件的 id 发生变化
所以我需要创建一个方法来访问在 onCreate() 中创建的所有对象,如按钮等。
public class Aero extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.aero);
final SlidingDrawer slider = (SlidingDrawer) findViewById(R.id.slidingDrawer1);
final TextView secondyear = (TextView) findViewById(R.id.ae2yrsyll);
final ScrollView scrl = (ScrollView) findViewById(R.id.scrollView1);
Button one = (Button) findViewById(R.id.ae_three_sem_am3);
Button two = (Button) findViewById(R.id.ae_three_sem_at);
Button three = (Button) findViewById(R.id.ae_three_sem_fmm);
Button four = (Button) findViewById(R.id.ae_three_sem_cp);
Button five = (Button) findViewById(R.id.ae_three_sem_ea);
one.setOnClickListener(new OnClickListener() {
public void mymethod(int resourse) {
scrl.pageScroll(View.FOCUS_UP);
InputStream is = getResources().openRawResource(resourse);
BufferedReader br = new BufferedReader(
new InputStreamReader(is));
String line;
String entireFile = "";
try {
while ((line = br.readLine()) != null) { // <--------- place
// readLine()
// inside loop
entireFile += (line + "\n"); // <---------- add each
// line to entireFile
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
secondyear.setText(entireFile); // <------- assign entireFile to
// TextView
if (slider.isOpened()) {
slider.close();
}
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
scrl.pageScroll(View.FOCUS_UP);
InputStream is = getResources().openRawResource(R.raw.aero1);
BufferedReader br = new BufferedReader(
new InputStreamReader(is));
String line;
String entireFile = "";
try {
while ((line = br.readLine()) != null) { // <--------- place
// readLine()
// inside loop
entireFile += (line + "\n"); // <---------- add each
// line to entireFile
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
secondyear.setText(entireFile); // <------- assign entireFile to
// TextView
if (slider.isOpened()) {
slider.close();
}
}
});
two.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
scrl.pageScroll(View.FOCUS_UP);
InputStream is = getResources().openRawResource(R.raw.aero2);
BufferedReader br = new BufferedReader(
new InputStreamReader(is));
String line;
String entireFile = "";
try {
while ((line = br.readLine()) != null) { // <--------- place
// readLine()
// inside loop
entireFile += (line + "\n"); // <---------- add each
// line to entireFile
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
secondyear.setText(entireFile); // <------- assign entireFile to
// TextView
if (slider.isOpened()) {
slider.close();
}
}
});
three.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
scrl.pageScroll(View.FOCUS_UP);
InputStream is = getResources().openRawResource(R.raw.aero3);
BufferedReader br = new BufferedReader(
new InputStreamReader(is));
String line;
String entireFile = "";
try {
while ((line = br.readLine()) != null) { // <--------- place
// readLine()
// inside loop
entireFile += (line + "\n"); // <---------- add each
// line to entireFile
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
secondyear.setText(entireFile); // <------- assign entireFile to
// TextView
if (slider.isOpened()) {
slider.close();
}
}
});
four.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
scrl.pageScroll(View.FOCUS_UP);
InputStream is = getResources().openRawResource(R.raw.aero4);
BufferedReader br = new BufferedReader(
new InputStreamReader(is));
String line;
String entireFile = "";
try {
while ((line = br.readLine()) != null) { // <--------- place
// readLine()
// inside loop
entireFile += (line + "\n"); // <---------- add each
// line to entireFile
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
secondyear.setText(entireFile); // <------- assign entireFile to
// TextView
if (slider.isOpened()) {
slider.close();
}
}
});
five.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
scrl.pageScroll(View.FOCUS_UP);
InputStream is = getResources().openRawResource(R.raw.aero5);
BufferedReader br = new BufferedReader(
new InputStreamReader(is));
String line;
String entireFile = "";
try {
while ((line = br.readLine()) != null) { // <--------- place
// readLine()
// inside loop
entireFile += (line + "\n"); // <---------- add each
// line to entireFile
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
secondyear.setText(entireFile); // <------- assign entireFile to
// TextView
if (slider.isOpened()) {
slider.close();
}
}
});
因此我想创建一个名为 myMethod 的方法,如下所示
myMethod(int reso){scrl.pageScroll(View.FOCUS_UP);
InputStream is = getResources().openRawResource(reso);
BufferedReader br = new BufferedReader(
new InputStreamReader(is));
String line;
String entireFile = "";
try {
while ((line = br.readLine()) != null) { // <--------- place
// readLine()
// inside loop
entireFile += (line + "\n"); // <---------- add each
// line to entireFile
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
secondyear.setText(entireFile); // <------- assign entireFile to
// TextView
if (slider.isOpened()) {
slider.close();
}}
我应该把这个方法放在哪里?
请帮忙
只需将您的视图保存到 Activity 中的变量中并访问它们。
public class Aero extends Activity {
TextView mSecondYear;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.aero);
mSecondYear = (TextView) findViewById(R.id.ae2yrsyll);
...
}
public void myMethod() {
mSecondYear.setText("bla");
}
}
我正在尝试开发一个应用程序,其中我有一组代码,每次按下 5 个按钮中的一个按钮时都必须调用这些代码。只有与之关联的要打开的文件的 id 发生变化 所以我需要创建一个方法来访问在 onCreate() 中创建的所有对象,如按钮等。
public class Aero extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.aero);
final SlidingDrawer slider = (SlidingDrawer) findViewById(R.id.slidingDrawer1);
final TextView secondyear = (TextView) findViewById(R.id.ae2yrsyll);
final ScrollView scrl = (ScrollView) findViewById(R.id.scrollView1);
Button one = (Button) findViewById(R.id.ae_three_sem_am3);
Button two = (Button) findViewById(R.id.ae_three_sem_at);
Button three = (Button) findViewById(R.id.ae_three_sem_fmm);
Button four = (Button) findViewById(R.id.ae_three_sem_cp);
Button five = (Button) findViewById(R.id.ae_three_sem_ea);
one.setOnClickListener(new OnClickListener() {
public void mymethod(int resourse) {
scrl.pageScroll(View.FOCUS_UP);
InputStream is = getResources().openRawResource(resourse);
BufferedReader br = new BufferedReader(
new InputStreamReader(is));
String line;
String entireFile = "";
try {
while ((line = br.readLine()) != null) { // <--------- place
// readLine()
// inside loop
entireFile += (line + "\n"); // <---------- add each
// line to entireFile
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
secondyear.setText(entireFile); // <------- assign entireFile to
// TextView
if (slider.isOpened()) {
slider.close();
}
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
scrl.pageScroll(View.FOCUS_UP);
InputStream is = getResources().openRawResource(R.raw.aero1);
BufferedReader br = new BufferedReader(
new InputStreamReader(is));
String line;
String entireFile = "";
try {
while ((line = br.readLine()) != null) { // <--------- place
// readLine()
// inside loop
entireFile += (line + "\n"); // <---------- add each
// line to entireFile
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
secondyear.setText(entireFile); // <------- assign entireFile to
// TextView
if (slider.isOpened()) {
slider.close();
}
}
});
two.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
scrl.pageScroll(View.FOCUS_UP);
InputStream is = getResources().openRawResource(R.raw.aero2);
BufferedReader br = new BufferedReader(
new InputStreamReader(is));
String line;
String entireFile = "";
try {
while ((line = br.readLine()) != null) { // <--------- place
// readLine()
// inside loop
entireFile += (line + "\n"); // <---------- add each
// line to entireFile
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
secondyear.setText(entireFile); // <------- assign entireFile to
// TextView
if (slider.isOpened()) {
slider.close();
}
}
});
three.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
scrl.pageScroll(View.FOCUS_UP);
InputStream is = getResources().openRawResource(R.raw.aero3);
BufferedReader br = new BufferedReader(
new InputStreamReader(is));
String line;
String entireFile = "";
try {
while ((line = br.readLine()) != null) { // <--------- place
// readLine()
// inside loop
entireFile += (line + "\n"); // <---------- add each
// line to entireFile
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
secondyear.setText(entireFile); // <------- assign entireFile to
// TextView
if (slider.isOpened()) {
slider.close();
}
}
});
four.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
scrl.pageScroll(View.FOCUS_UP);
InputStream is = getResources().openRawResource(R.raw.aero4);
BufferedReader br = new BufferedReader(
new InputStreamReader(is));
String line;
String entireFile = "";
try {
while ((line = br.readLine()) != null) { // <--------- place
// readLine()
// inside loop
entireFile += (line + "\n"); // <---------- add each
// line to entireFile
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
secondyear.setText(entireFile); // <------- assign entireFile to
// TextView
if (slider.isOpened()) {
slider.close();
}
}
});
five.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
scrl.pageScroll(View.FOCUS_UP);
InputStream is = getResources().openRawResource(R.raw.aero5);
BufferedReader br = new BufferedReader(
new InputStreamReader(is));
String line;
String entireFile = "";
try {
while ((line = br.readLine()) != null) { // <--------- place
// readLine()
// inside loop
entireFile += (line + "\n"); // <---------- add each
// line to entireFile
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
secondyear.setText(entireFile); // <------- assign entireFile to
// TextView
if (slider.isOpened()) {
slider.close();
}
}
});
因此我想创建一个名为 myMethod 的方法,如下所示
myMethod(int reso){scrl.pageScroll(View.FOCUS_UP);
InputStream is = getResources().openRawResource(reso);
BufferedReader br = new BufferedReader(
new InputStreamReader(is));
String line;
String entireFile = "";
try {
while ((line = br.readLine()) != null) { // <--------- place
// readLine()
// inside loop
entireFile += (line + "\n"); // <---------- add each
// line to entireFile
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
secondyear.setText(entireFile); // <------- assign entireFile to
// TextView
if (slider.isOpened()) {
slider.close();
}}
我应该把这个方法放在哪里? 请帮忙
只需将您的视图保存到 Activity 中的变量中并访问它们。
public class Aero extends Activity {
TextView mSecondYear;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.aero);
mSecondYear = (TextView) findViewById(R.id.ae2yrsyll);
...
}
public void myMethod() {
mSecondYear.setText("bla");
}
}