2015-04-01 48 views
1

如何在一個活動中使用java類,我的意思是將該活動的不同組件分散在一堆java類中。我有點新的Android和這是我到目前爲止已經試過:課程在主要活動內部不會正確執行?

MainActivity.java

package com.example.alex.myapplication; 



import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 



public class MainActivity extends ActionBarActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    new Something(this); 


} 





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

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 
    } 

Something.java

import android.view.View; 
import android.widget.EditText; 
import android.widget.Button; 
import android.app.Activity; 




    public class Something { 

    private Activity activity; 
    private Button add,subtract,multiply,devide; 
    private EditText editA, editB, editC; 
    private double doubleA,doubleB,doubleC; 





     public Something(Activity a){ 

       activity=a; 
       click(); 

     } 


     public void click(){ 
      editA = (EditText) activity.findViewById(R.id.editText); 
      editB = (EditText) activity.findViewById(R.id.editText2); 
      editC = (EditText) activity.findViewById(R.id.editText3); 
      doubleA =Double.parseDouble(editA.getText().toString()); 
      doubleB =Double.parseDouble(editB.getText().toString()); 


       add = (Button) activity.findViewById(R.id.add); 
       subtract = (Button) activity.findViewById(R.id.subtract); 
       multiply = (Button) activity.findViewById(R.id.multiply); 
       devide = (Button) activity.findViewById(R.id.devide); 

       add.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 

        doubleC = doubleA+doubleB; 
        String s = "" + doubleC; 
         editC.setText(s); 


        } 
       }); 

       subtract.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 

        doubleC = doubleA-doubleB; 
        String s = "" + doubleC; 
        editC.setText(s); 


       } 
      }); 

       multiply.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 

        doubleC = doubleA*doubleB; 
        String s = "" + doubleC; 
        editC.setText(s); 


       } 
      }); 



      devide.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 

        doubleC = doubleA/doubleB; 
        String s = "" + doubleC; 
        editC.setText(s); 


       } 
      }); 



     } 



     } 

所以我不確定爲什麼我的聽衆不在我的按鈕上工作,所以我嘗試將活動傳遞給該類聽衆添加到按鈕,但實際上現在不工作,現在我的應用程序甚至不會在模擬器中啓動。我想要做的就是讓「MainActivity」處理「Gui」,並讓「Something」類處理聽衆,但無論我做什麼,我都無法讓他們彼此通信以形成一個Activity。

有人建議在主要活動Something s = new Something(MainActivity.this);中通過並從主要活動內部調用它,如s.click();但這不起作用。

+0

問題是什麼?你的程序崩潰了嗎?如果它崩潰,您在LogCat中獲得什麼信息? – immibis 2015-04-01 21:10:57

+0

您的Something Class正在使用Activity調用,但您尚未擴展它。 – Aizen 2015-04-01 21:19:08

+0

請不要一遍又一遍地提出同樣的問題,而是使用[這個問題](http://stackoverflow.com/questions/29380427/how-to-use-java-classes-within-one-activity)。 – nhaarman 2015-04-01 21:24:50

回答

0

在android中,每個擴展activity的類都只有一個xml Layout來查看和處理這個XML文件就在它的Activities中,並且可以製作大量的發送大量參數的java類,但是當您想要處理XML行爲你應該在自己的班級做延伸活動,所以你可以這樣做:

public class MainActivity extends ActionBarActivity { 

Button myBtn; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 
    new Something(this); 
    myBtn=(Button)findViewById(R.id.Btn); 
    myBtn.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

     } 
    }); 

} 

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

//noinspection SimplifiableIfStatement 
if (id == R.id.action_settings) { 
    return true; 
} 

return super.onOptionsItemSelected(item); 
}