1

当我按下应用程序上的任何按钮时,该值不会出现在 edittext 上并且没有操作操作。我使用了另一个类似的[项目]:http ://android.okhelp.cz/dw/Calculator.apk我改变了一些东西。原始项目可以正常工作 http://android.okhelp.cz/calculator-open-source/

<?xml version="1.0" encoding="utf-8"?>
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="fill_parent"
        android:layout_height="130dp"
        android:clickable="false"
        android:cursorVisible="false"
        android:gravity="right"
       />
</LinearLayout>

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="5dp"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/button_sin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClickListener_sin"
        android:clickable="true"
        android:text="sin" />

    <Button
        android:id="@+id/button_cos"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClickListener_cos"
        android:clickable="true"
        android:text="cos" />

    <Button
        android:id="@+id/button_del"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1.40"
        android:onClick="onClickListenerReset"
        android:text="Delete" />

</LinearLayout>

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/button_tan"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="onClickListener_tan"
        android:text="tan" />

    <Button
        android:id="@+id/button7"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:onClick="onClickListener7"
        android:text="7" />

    <Button
        android:id="@+id/button8"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:onClick="onClickListener8"
        android:text="8" />

    <Button
        android:id="@+id/button9"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="1.00"
        android:onClick="onClickListener9"
        android:text="9" />

    <Button
        android:id="@+id/buttonDivide"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="1.00"
        android:onClick="onClickListenerDivide"
        android:text="/" />
</LinearLayout>

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/button_log"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="onClickListener_log"
        android:text="log" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:onClick="onClickListener4"
        android:text="4" />

    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:onClick="onClickListener5"
        android:text="5" />

    <Button
        android:id="@+id/button6"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="1.00"
        android:onClick="onClickListener6"
        android:text="6" />

    <Button
        android:id="@+id/buttonMultiply"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="1.00"
        android:onClick="onClickListenerMultiply"
        android:text="*" />
</LinearLayout>

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/button_bra1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="onClickListener_bra1"
        android:text="(" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:onClick="onClickListener1"
        android:text="1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:onClick="onClickListener2"
        android:text="2" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="1.00"
        android:onClick="onClickListener3"
        android:text="3" />

    <Button
        android:id="@+id/buttonMinus"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="1.00"
        android:onClick="onClickListenerMinus"
        android:text="-" />
</LinearLayout>

<LinearLayout
    android:id="@+id/linearLayout3"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/button_bra2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="onClickListener_bra2"
        android:text=")" />

    <Button
        android:id="@+id/buttonPoint"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="onClickListenerPoint"
        android:text="." />

    <Button
        android:id="@+id/button0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="onClickListener0"
        android:text="0" />

    <Button
        android:id="@+id/buttonEqual"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="onClickListenerEqual"
        android:text="="
        android:textSize="15sp" />

    <Button
        android:id="@+id/buttonPlus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="onClickListenerPlus"
        android:text="+"
        android:textSize="15dp" />
</LinearLayout>

jave 文件

package calc.pack;


import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;

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


public class MainActivity extends Activity {
EditText editText;

Button button0,button1,button2,button3,
       button4,button5,button6,button7,button8,button9,buttonPlus,buttonMinus,buttonMultiply,
            buttonDivide,buttonEqual,buttonPoint,buttonDel,buttonReset,
            button_sin,button_cos,button_tan,button_del,button_bra1,button_bra2,button_reset;

String sum="",one,two,three,four,five,
                        six,seven,eight,nine,
                        zero,plus,minus,multiply,divide,equal,point,del,reset,bra1,bra2;

Integer countOne=0;

Float result=0f,result_mul=1f,result_div=1f;

int pressCount=1,sumZero,c,i;

char press;

String EditTextMsg;

Float floatEditTextMsg;

Double after_log,doubleEditTextMsg,afterSin,after_cos,after_tan,toRadian_doubleEditTextMsg;

Vibrator vibrator;
Bundle savedInstanceState;

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




    editText=(EditText)findViewById(R.id.editText1);

    button0=(Button)findViewById(R.id.button0);
    button1=(Button)findViewById(R.id.button1);
    button2=(Button)findViewById(R.id.button2);
    button3=(Button)findViewById(R.id.button3);
    button4=(Button)findViewById(R.id.button4);

    button5=(Button)findViewById(R.id.button5);
    button6=(Button)findViewById(R.id.button6);
    button7=(Button)findViewById(R.id.button7);
    button8=(Button)findViewById(R.id.button8);
    button9=(Button)findViewById(R.id.button9);

    buttonPlus=(Button)findViewById(R.id.buttonPlus);
    buttonMinus=(Button)findViewById(R.id.buttonMinus);
    buttonMultiply=(Button)findViewById(R.id.buttonMultiply);
    buttonDivide=(Button)findViewById(R.id.buttonDivide);
    buttonPoint=(Button)findViewById(R.id.buttonPoint);

    buttonEqual=(Button)findViewById(R.id.buttonEqual);

    button_sin=(Button)findViewById(R.id.button_sin);
    button_cos=(Button)findViewById(R.id.button_cos);
    button_tan=(Button)findViewById(R.id.button_tan);

    button_del=(Button)findViewById(R.id.button_del);

    vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); 


    editText.setText(result.toString());


}
public void onClickListener0(View v)
{
    vibrator.vibrate(30);
    if(press=='=')
    {
        onClickListenerReset(buttonReset);
    }

    if(sum !="")
    {
        zero=(String)button0.getText();

        sum=sum+zero;
        editText.setText(sum);
    }
    else
    {
        sum="0";
        editText.setText("0");
    }
}

public void onClickListener1(View v)
{
    vibrator.vibrate(30);

    if(press=='=')
    {
        onClickListenerReset(buttonReset);
    }

    one=(String)button1.getText();
    sum=sum+one;

    editText.setText(sum);
}

public void onClickListener2(View v)
{
    vibrator.vibrate(30);

    if(press=='=')
    {
        onClickListenerReset(buttonReset);
    }

    if(press=='=')
    {
        onClickListenerEqual(buttonEqual);
    }

    two=(String)button2.getText();
    sum=sum+two;

    editText.setText(sum);
}

public void onClickListener3(View v)
{
    vibrator.vibrate(30);
    if(press=='=')
    {
        onClickListenerReset(buttonReset);
    }
    three=(String)button3.getText();
    sum=sum+three;

    editText.setText(sum);
}

public void onClickListener4(View v)
{
    vibrator.vibrate(30);

    if(press=='=')
    {
        onClickListenerReset(buttonReset);
    }

    four=(String)button4.getText();
    sum=sum+four;

    editText.setText(sum);
}

public void onClickListener5(View v)
{
    vibrator.vibrate(30);
    if(press=='=')
    {
        onClickListenerReset(buttonReset);
    }

    five=(String)button5.getText();
    sum=sum+five;

    editText.setText(sum);
}

public void onClickListener6(View v)
{
    vibrator.vibrate(30);
    if(press=='=')
    {
        onClickListenerReset(buttonReset);
    }

    six=(String)button6.getText();
    sum=sum+six;

    editText.setText(sum);
}

public void onClickListener7(View v)
{
    vibrator.vibrate(30);
    if(press=='=')
    {
        onClickListenerReset(buttonReset);
    }

    seven=(String)button7.getText();
    sum=sum+seven;

    editText.setText(sum);
}

public void onClickListener8(View v)
{
    vibrator.vibrate(30);
    if(press=='=')
    {
        onClickListenerReset(buttonReset);
    }

    eight=(String)button8.getText();
    sum=sum+eight;

    editText.setText(sum);
}

public void onClickListener9(View v)
{
    vibrator.vibrate(30);
    if(press=='=')
    {
        onClickListenerReset(buttonReset);
    }

    nine=(String)button9.getText();
    sum=sum+nine;

    editText.setText(sum);
}

public void onClickListenerPlus(View v)
{
    vibrator.vibrate(30);

    if(press=='-')
    {
        onClickListenerEqual(buttonMinus);
    }
    else if(press=='*')
    {
        onClickListenerEqual(buttonMultiply);
    }

    else if(press=='/')
    {
        onClickListenerEqual(buttonDivide);
    }


    press='+';

    if(sum != "")
    {
        result=result+Float.parseFloat(editText.getText().toString());

        editText.setText(result.toString());

        result_mul=result;

        result_div=result;

        sum="";            
    }
    else
    {
        editText.setText(result.toString());

        result_mul=result;

        result_div=result;

        sum="";
    }

}

public void onClickListenerMinus(View v)
{
    vibrator.vibrate(30);
    if(press=='+')
    {
        onClickListenerEqual(buttonPlus);
    }
    else if(press=='*')
    {
        onClickListenerEqual(buttonMultiply);
    }
    else if(press=='/')
    {
        onClickListenerEqual(buttonDivide);
    }



    press='-';

    EditTextMsg= editText.getText().toString(); 
    floatEditTextMsg=Float.parseFloat(EditTextMsg);

    if(sum==""  && result==0)
    {
        sum=sum+'-';
        //Log.d("sum=","minus press");
    }
    else if(sum != "")
    {
        if(result==0)
        {
            result=Float.parseFloat(sum)-result;

            editText.setText(result.toString());

            result_mul=result;

            result_div=result;

            sum="";
        }

        else
        {
            result=result-Float.parseFloat(sum);

            editText.setText(result.toString());

            result_mul=result;

            result_div=result;

            sum="";
        }
    }

}

public void onClickListenerMultiply(View v)
{
    vibrator.vibrate(30);
    if(press=='/')
    {
        onClickListenerEqual(buttonDivide);
    }
    else if(press=='+')
    {
        onClickListenerEqual(buttonPlus);
    }

    else if(press=='-')
    {
        onClickListenerEqual(buttonMinus);
    }



    press='*';

    EditTextMsg= editText.getText().toString(); 
    floatEditTextMsg=Float.parseFloat(EditTextMsg);

    if(sum !="")
    {
        result_mul=result_mul * floatEditTextMsg; 

        result=result_mul;

        result_div=result_mul;

        editText.setText(result_mul.toString());

        sum="";
    }
    else
    {
        editText.setText(EditTextMsg);

        //result_mul=result_mul * Float.parseFloat(sum);

        //result=result_mul;

        sum="";
    }

}


public void onClickListenerDivide(View v)
{
    vibrator.vibrate(30);
    if(press=='+')
    {
        onClickListenerEqual(buttonPlus);
    }

    else if(press=='-')
    {
        onClickListenerEqual(buttonMinus);
    }

    else if(press=='*')
    {
        onClickListenerEqual(buttonMultiply);
    }

    press='/';

    EditTextMsg= editText.getText().toString(); 
    floatEditTextMsg=Float.parseFloat(EditTextMsg);

    if(sum !="" && result_div==1)
    {
        //int c=0;

        if(c==0)
        {
            result_div=floatEditTextMsg/result_div;  
            Log.d("if if result_div=", result_div.toString());
            c++;
        }
        else
        {
            result_div=result_div/floatEditTextMsg; 
            Log.d("if else result_div=", result_div.toString());
        }


        result=result_div;
        result_mul=result_div;

        editText.setText(result_div.toString());

        sum="";
    }
    else if(sum !="" && result_div !=1)
    {
        result_div=result_div/floatEditTextMsg; 

        Log.d("else if result_div=", result_div.toString());

        result=result_div;

        result_mul=result_div;

        editText.setText(result_div.toString());

        sum="";
    }
    else
    {
        editText.setText(EditTextMsg);

        sum="";
    }
}



public void onClickListenerPoint(View v)
{
    vibrator.vibrate(30);

   int error=0;

    if(sum !=null)
    {
        for(int i=0;i<sum.length();i++)
        {
            if(sum.charAt(i)=='.')
            {
                error=1;
                break;
            }
        }

    }

    if(error==0)
    {
        if(sum==null)
        {
            sum=sum+"0.";
        }
        else
        {
            sum=sum+".";
        }
    }

    editText.setText(sum);
}

public void onClickListenerEqual(View v)
{
    vibrator.vibrate(30);

    if(press=='+') 
    {
        onClickListenerPlus(buttonPlus);
        //msg1= editText.getText().toString(); 
        //floatMsg=Float.parseFloat(msg1);
    }
    else if(press=='-') 
    {
        onClickListenerMinus(buttonMinus);
    }
    else if(press=='*') 
    {
        onClickListenerMultiply(buttonMultiply);
    }
    else if(press=='/')
    {
        onClickListenerDivide(buttonDivide);
    }

    press='=';


}



public void onClickListenerReset(View v)
{
    vibrator.vibrate(30);

    sum="";
    countOne=0;//result=0;

    result=0f;
    result_mul=1f;
    result_div=1f;
    press=' ';
    c=0;

    editText.setText(result.toString());
}


public void onClickListener_sin(View v)
{
    vibrator.vibrate(30);

    EditTextMsg= editText.getText().toString(); 
    doubleEditTextMsg=Double.parseDouble(EditTextMsg);//degree

    toRadian_doubleEditTextMsg=Math.toRadians(doubleEditTextMsg);

    afterSin =Math.sin(toRadian_doubleEditTextMsg);

    editText.setText(afterSin.toString());

    EditTextMsg=editText.getText().toString();
    result=Float.parseFloat(EditTextMsg);

    result_mul=Float.parseFloat(EditTextMsg);

    result_div=Float.parseFloat(EditTextMsg);


    sum="";


}

public void onClickListener_cos(View v)
{
    vibrator.vibrate(30);
    EditTextMsg= editText.getText().toString(); 
    doubleEditTextMsg=Double.parseDouble(EditTextMsg);//degree

    toRadian_doubleEditTextMsg=Math.toRadians(doubleEditTextMsg);

    after_cos =Math.cos(toRadian_doubleEditTextMsg);

    editText.setText(after_cos.toString());

    EditTextMsg=editText.getText().toString();
    result=Float.parseFloat(EditTextMsg);

    result_mul=Float.parseFloat(EditTextMsg);

    result_div=Float.parseFloat(EditTextMsg);


    sum="";


}

public void onClickListener_tan(View v)
{
    vibrator.vibrate(30);

    EditTextMsg= editText.getText().toString(); 
    doubleEditTextMsg=Double.parseDouble(EditTextMsg);//degree

    toRadian_doubleEditTextMsg=Math.toRadians(doubleEditTextMsg);

    after_tan =Math.tan(toRadian_doubleEditTextMsg);

    editText.setText(after_tan.toString());

    EditTextMsg=editText.getText().toString();
    result=Float.parseFloat(EditTextMsg);

    result_mul=Float.parseFloat(EditTextMsg);

    result_div=Float.parseFloat(EditTextMsg);


    sum="";
}
public void onClickListener_log(View v)
{

    vibrator.vibrate(30);

   EditTextMsg= editText.getText().toString(); 
   doubleEditTextMsg=Double.parseDouble(EditTextMsg);//degree

   after_log =Math.log10(doubleEditTextMsg);

   editText.setText(after_log.toString());

   EditTextMsg=editText.getText().toString();
   result=Float.parseFloat(EditTextMsg);

   result_mul=Float.parseFloat(EditTextMsg);

   result_div=Float.parseFloat(EditTextMsg);


   sum="";


}


public void onClickListener_bra1(View v)
{

   vibrator.vibrate(30);


    bra1=(String)button_bra1.getText();

    sum=sum+"(";
    editText.setText(sum);

}

public void onClickListener_bra2(View v)
{



    bra2=(String)button_bra2.getText();

    sum=sum+")";
    editText.setText(sum);
} 




 public void onClickListener_del(View v)
 {
     vibrator.vibrate(30);
     if(sum !="")
     {
         StringBuilder stringBuilder=new StringBuilder(80);

         stringBuilder.append(sum);

         sum=stringBuilder.deleteCharAt(stringBuilder.length()-1).toString();

         editText.setText(sum);
     }

 }

}

没有 LogCat 。

4

2 回答 2

4

您可以为您拥有的类实现 onClickListener,以便处理单击事件的常用方法,然后您必须将按钮注册到 button1.setOnClickListener(this)。

于 2013-03-23T05:28:20.377 回答
0

您必须将 setonclickListner() 方法放入 button.eg 的对象

 button0.setOnClickListner();
于 2013-03-23T06:01:28.700 回答