0

我正在制作一个计算器,并且我添加了一个新的运算符 1/x 来反转数字。当用户从微调器中选择此运算符时,应立即禁用第二个编辑文本,但我不知道如何执行此操作。请帮我!这是我的代码 -

activity_main.xml

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

<EditText

    android:id="@+id/edtNumber1"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:inputType="numberDecimal"

    android:hint="@string/hntNumber1"
/>

<Spinner

    android:id="@+id/sprOperator"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:entries="@array/sprItems"
/>

<EditText

    android:id="@+id/edtNumber2"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:inputType="numberDecimal"

    android:hint="@string/hntNumber2"
/>
<Button
    android:id="@+id/btnResult"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="@string/lblBtnResult"
/>
<EditText
    android:id="@+id/edtResult"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:inputType="numberDecimal"

/>

<Button
    android:id="@+id/buttonAlert"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="EXIT"
     />

</LinearLayout>

MainActivity.java

package com.example.my_cal;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends Activity {

final Context context = this;

private Button button;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);


    // Defining onclick listener for finding the result

    OnClickListener listener = new OnClickListener() {

       @Override

       public void onClick(View v) {

            Spinner spr = (Spinner) findViewById(R.id.sprOperator);

            EditText edtNumber1 = ( EditText) findViewById(R.id.edtNumber1);

            EditText edtNumber2 = ( EditText) findViewById(R.id.edtNumber2);

            EditText edtResult = ( EditText) findViewById(R.id.edtResult);

            String selectedItem = (String) spr.getSelectedItem();

            if(selectedItem.trim().equals("+")){

                float result = Float.parseFloat(edtNumber1.getText().toString())  + Float.parseFloat(edtNumber2.getText().toString());

edtResult.setText(Float.toString(result));

            }else if(selectedItem.trim().equals("-")){

                float result = Float.parseFloat(edtNumber1.getText().toString())  - Float.parseFloat(edtNumber2.getText().toString());

                edtResult.setText(Float.toString(result));

            }else if(selectedItem.trim().equals("x")){

                float result = Float.parseFloat(edtNumber1.getText().toString())  * Float.parseFloat(edtNumber2.getText().toString());

                edtResult.setText(Float.toString(result));

            }else if(selectedItem.trim().equals("/")){

                float result = Float.parseFloat(edtNumber1.getText().toString())  / Float.parseFloat(edtNumber2.getText().toString());

                edtResult.setText(Float.toString(result));

            }

            else if(selectedItem.trim().equals("1/x")){

float result = 1  / Float.parseFloat(edtNumber1.getText().toString());

                edtResult.setText(Float.toString(result));
            }
       }

    };

    button = (Button) findViewById(R.id.buttonAlert);

    // add button listener

    button.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {

        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
            context);

        // set title
        alertDialogBuilder.setTitle("Your Title");

        // set dialog message
        alertDialogBuilder

            .setMessage("Click yes to exit!")

            .setCancelable(false)

            .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {
                    // if this button is clicked, close
                    // current activity
                    MainActivity.this.finish();
                }
              })
            .setNegativeButton("No",new 

DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog,int id) {
                    // if this button is clicked, just close
                    // the dialog box and do nothing
                    dialog.cancel();
                }
            });

            // create alert dialog

            AlertDialog alertDialog = alertDialogBuilder.create();

            // show it
            alertDialog.show();
        }
    });
    button = (Button) findViewById(R.id.buttonAlert);

    // add button listener
    button.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {

        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
            context);

        // set title
        alertDialogBuilder.setTitle("EXIT");

        // set dialog message
        alertDialogBuilder
            .setMessage("Are you sure want to exit ?")
            .setCancelable(false)
            .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {
                    // if this button is clicked, close
                    // current activity
                    MainActivity.this.finish();
                }
              })
            .setNegativeButton("No",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {
                    // if this button is clicked, just close
                    // the dialog box and do nothing
                    dialog.cancel();
                }
            });

            // create alert dialog
            AlertDialog alertDialog = alertDialogBuilder.create();

            // show it
            alertDialog.show();
        }
    });

    // Getting reference of the button btnResult
    Button btn = (Button) findViewById(R.id.btnResult);

    // Setting onclick listener
    btn.setOnClickListener(listener);

}
}
4

3 回答 3

1

我不太明白你的意思,但你可以隐藏编辑文本

 edtNumber2.setVisibility(View.GONE);

或者

edtNumber2.setVisibility(View.INVISIBLE);

并再次展示它

edtNumber2.setVisibility(View.VISIBLE);
于 2013-03-16T05:26:08.850 回答
0

做这个,

    EditText edit = (EditText) findViewById(R.id.EditText01);
    edit.setEnabled(false);
    edit.setInputType(InputType.TYPE_NULL);
    edit.setFocusable(false);

And when any other option selected do this,

edit.setEnabled(true);
edit.setInputType(InputType.TYPE_CLASS_TEXT);
edit.setFocusable(true);
于 2013-03-16T06:30:27.463 回答
0

用这个edtNumber2.setEnabled(false);

在你的情况下

else if(selectedItem.trim().equals("1/x")){

edtNumber2.setEnabled(false);

float result = 1  / Float.parseFloat(edtNumber1.getText().toString());

                edtResult.setText(Float.toString(result));
            }

编辑

我重写了您的部分代码以实现您想要的

    public class MainActivity extends Activity {

final Context context = this;

private Button button;
private EditText edtNumber1;
private EditText edtNumber2;
private Spinner spr;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

        spr = (Spinner) findViewById(R.id.sprOperator);

        edtNumber1 = ( EditText) findViewById(R.id.edtNumber1);

        edtNumber2 = ( EditText) findViewById(R.id.edtNumber2);

        edtResult = ( EditText) findViewById(R.id.edtResult);


        spr.setOnItemSelectedListener(
            new OnItemSelectedListener() {
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    String selectedItem = parent.getItemAtPosition(position).toString();
                    if(selectedItem.trim().equals("1/x")){
                        edtNumber2.setEnabled(false);
                    }else{
                        edtNumber2.setEnabled(true);
                    }                       
                }

                public void onNothingSelected(AdapterView<?> parent) {

                }
            });

// Defining onclick listener for finding the result

OnClickListener listener = new OnClickListener() {

   @Override

   public void onClick(View v) {

        String selectedItem = (String) spr.getSelectedItem();
        if(selectedItem.trim().equals("+")){
            float result = Float.parseFloat(edtNumber1.getText().toString())  + Float.parseFloat(edtNumber2.getText().toString());
            edtResult.setText(Float.toString(result));
        }else if(selectedItem.trim().equals("-")){
            float result = Float.parseFloat(edtNumber1.getText().toString())  - Float.parseFloat(edtNumber2.getText().toString());
            edtResult.setText(Float.toString(result));
        }else if(selectedItem.trim().equals("x")){
            float result = Float.parseFloat(edtNumber1.getText().toString())  * Float.parseFloat(edtNumber2.getText().toString());
            edtResult.setText(Float.toString(result));
        }else if(selectedItem.trim().equals("/")){
            float result = Float.parseFloat(edtNumber1.getText().toString())  / Float.parseFloat(edtNumber2.getText().toString());
            edtResult.setText(Float.toString(result));
        } else if(selectedItem.trim().equals("1/x")){
            float result = 1  / Float.parseFloat(edtNumber1.getText().toString());
            edtResult.setText(Float.toString(result));
        }


   }

    };
.
.
.
.

希望这对您有所帮助。

于 2013-03-16T05:28:36.387 回答