我正在制作一个计算器,并且我添加了一个新的运算符 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);
}
}