2016-07-28 14 views
0

ボタンが押されたときに呼び出されるコードセットがあるアプリケーションを開発しようとしています。のみ、それに関連付けられた開くファイルのIDは、私がのonCreate(で作成されたすべてのオブジェクトにアクセスすることができますメソッドを作成する必要があるので、 を変更)ボタンのようなど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(); 
      } 
     } 
    }); 

私が作成したいtherfore次のような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(); 
      }} 

どこにこのメソッドを配置しますか? 助けてください

答えて

1

あなたのビューをアクティビティの変数に保存し、アクセスしてください。

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"); 
    } 
} 
+0

ありがとうございます。 ..実際に働いた。 –

関連する問題