我正在尝试编写一个计算密度的类,您也可以选择密度或质量或体积。我设置好像你选择以上任何值相关的edittext得到setEnabled false并按下计算按钮设置文本在同一个edittext但它不起作用请帮助
这是我的java类: 包ir.kiandroid.chemcalculator3;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class Chegali extends Activity {
private static final int MENU_ITEM1 = 0;
private static final int MENU_ITEM2 = 1;
private static final int MENU_ITEM3 = 2;
// step1 : create all the variables.
EditText roe, me, ve;
TextView sub;
Button b1, b2;
// String phEvar;
RadioButton ro, m, vr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chegali);
// step2 : get all the views from xml file.
roe = (EditText) findViewById(R.id.e1);
me = (EditText) findViewById(R.id.e2);
ve = (EditText) findViewById(R.id.e3);
ro = (RadioButton) findViewById(R.id.one);
m = (RadioButton) findViewById(R.id.two);
vr = (RadioButton) findViewById(R.id.three);
sub = (TextView) findViewById(R.id.subject);
b1 = (Button) findViewById(R.id.button1);
b2 = (Button) findViewById(R.id.button2);
// /////////
sub.setText("محاسبه چگالی، ابتدا مجهول را انتخاب کنید سپس متغییر ها را وارد کنید.");
// ////////
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.rg);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (ro.isChecked() == true) {
roe.setText("X=? gr/ml");
me.setText("");
ve.setText("");
roe.setEnabled(false);
me.setEnabled(true);
ve.setEnabled(true);
} else if (m.isChecked() == true) {
roe.setText("");
me.setText("X=? gr");
ve.setText("");
me.setEnabled(false);
roe.setEnabled(true);
ve.setEnabled(true);
} else if (vr.isChecked() == true) {
roe.setText("");
me.setText("");
ve.setText("X=? ml");
ve.setEnabled(false);
roe.setEnabled(true);
me.setEnabled(true);
}
}
});
// step3 : write add functionality.
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
String roVar = roe.getText().toString();
double roDou = Double.parseDouble(roVar);
String vVar = ve.getText().toString();
double vDou = Double.parseDouble(vVar);
String mVar = me.getText().toString();
double mDou = Double.parseDouble(mVar);
if (ro.isChecked() == true) {
double result = mDou / vDou;
result = result * 100;
result = (double) ((int) result);
result = result / 100;
String rec = String.valueOf(result);
roe.setText("X=" + rec + "gr/ml");
} else if (m.isChecked() == true) {
double result = roDou * vDou;
result = result * 100;
result = (double) ((int) result);
result = result / 100;
String rec = String.valueOf(result);
me.setText("X=" + rec + "gr");
} else if (vr.isChecked() == true) {
double result = mDou / roDou;
result = result * 100;
result = (double) ((int) result);
result = result / 100;
String rec = String.valueOf(result);
ve.setText("X=" + rec + "ml");
} else {
Toast.makeText(Chegali.this, "مجهول را انتخاب کنید.",
Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
Toast.makeText(Chegali.this, "همه بخش ها را پر کنید.",
Toast.LENGTH_SHORT).show();
}
}
});
/*
*
*
* b2.setOnClickListener(new OnClickListener() {
*
* @Override public void onClick(View v) { String f =
* ph.getText().toString(); int i = Integer.parseInt(f); String s =
* volum.getText().toString(); int j = Integer.parseInt(s);
*
* Integer result = i-j; String res = result.toString();
* et3.setText(res); } });
*/
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
menu.add(Menu.NONE, MENU_ITEM1, Menu.NONE, "کیان دروید").setIcon(
R.drawable.kian);
menu.add(Menu.NONE, MENU_ITEM2, Menu.NONE, "درباره اپ").setIcon(
R.drawable.about);
menu.add(Menu.NONE, MENU_ITEM3, Menu.NONE, "فیسبوک").setIcon(
R.drawable.facebook);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case MENU_ITEM1:
Toast.makeText(getApplicationContext(), "کیان دروید",
Toast.LENGTH_LONG).show();
break;
case MENU_ITEM2:
startActivity(new Intent(Chegali.this, About.class));
Toast.makeText(getApplicationContext(), "درباره این برنامه",
Toast.LENGTH_LONG).show();
case MENU_ITEM3:
Toast.makeText(getApplicationContext(), "فیس بوک",
Toast.LENGTH_LONG).show();
}
return super.onOptionsItemSelected(item);
}
}
和xml文件:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@drawable/back3"
tools:context=".Chegali" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<ImageView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:src="@drawable/top" />
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_marginBottom="7dp"
android:background="@android:color/darker_gray"/>
<TextView
android:id="@+id/subject"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="عنوان"
android:textColor="#ffffff"
android:layout_marginBottom="7dp"
android:layout_gravity="right"
android:background="@drawable/tv2_background"
android:gravity="right"
android:textAppearance="?android:attr/textAppearanceLarge" />
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="@android:color/darker_gray"/>
<RadioGroup
android:id="@+id/rg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="10dp"
>
<RadioButton
android:id="@+id/one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:text="چگالی"
android:layout_gravity="right"
android:gravity="right"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
<EditText
android:id="@+id/e1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:background="@drawable/edit_text"
android:textCursorDrawable="@null"
android:textAppearance="?android:attr/textAppearanceLarge"
android:ems="8"
android:inputType="number|numberDecimal|numberSigned"
android:digits="0123456789."/>
<requestFocus />
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="@android:color/darker_gray"/>
<RadioButton
android:id="@+id/two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:layout_gravity="right"
android:gravity="right"
android:text="جرم"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
<EditText
android:id="@+id/e2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:background="@drawable/edit_text"
android:textCursorDrawable="@null"
android:textAppearance="?android:attr/textAppearanceLarge"
android:ems="8"
android:inputType="number|numberDecimal|numberSigned"
android:digits="0123456789."/>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="@android:color/darker_gray" />
<RadioButton
android:id="@+id/three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:layout_gravity="right"
android:gravity="right"
android:text="حجم"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
<EditText
android:id="@+id/e3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:background="@drawable/edit_text"
android:textCursorDrawable="@null"
android:textAppearance="?android:attr/textAppearanceLarge"
android:ems="8"
android:inputType="number|numberDecimal|numberSigned"
android:digits="0123456789."/>
</RadioGroup>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="@android:color/darker_gray"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right">
<Button
android:id="@+id/button1"
android:textAppearance="?android:attr/textAppearanceLargeInverse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_toRightOf="@+id/button2"
android:background="@drawable/button_shape"
android:textColor="#ffffff"
android:text="محاسبه" />
<Button
android:id="@+id/button2"
android:textAppearance="?android:attr/textAppearanceLargeInverse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginRight="20dp"
android:background="@drawable/button_shape"
android:textColor="#ffffff"
android:text="کمک" />
</RelativeLayout>
</LinearLayout>
</ScrollView>