班级在主要活动中不能正常执行?

时间:2015-04-01 21:02:47

标签: java android

如何在一个活动中使用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();,但这不起作用。

1 个答案:

答案 0 :(得分:0)

在android中,每个扩展活动的类只有一个xml布局来查看和处理这个XML文件就在它的活动中,你可以创建很多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);
}