我正在制作一个计算器,我添加了一个新的运算符1 / x,它反转了数字。当用户从微调器中选择此运算符时,应立即禁用第二个edittext但我没有得到如何执行此操作。请帮我!这是我的代码 -
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);
}
}
答案 0 :(得分:1)
我不太明白你的意思,但你可以通过
隐藏编辑文本 edtNumber2.setVisibility(View.GONE);
或
edtNumber2.setVisibility(View.INVISIBLE);
并再次显示
edtNumber2.setVisibility(View.VISIBLE);
答案 1 :(得分: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));
}
}
};
.
.
.
.
希望这有助于你。
答案 2 :(得分:0)
这样做,
EditText edit = (EditText) findViewById(R.id.EditText01);
edit.setEnabled(false);
edit.setInputType(InputType.TYPE_NULL);
edit.setFocusable(false);
当选择任何其他选项时,
edit.setEnabled(true);
edit.setInputType(InputType.TYPE_CLASS_TEXT);
edit.setFocusable(true);