[syntax=java5]package com.example.free.calc;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import java.math.BigDecimal;
import java.math.RoundingMode;
public class MainActivity extends ActionBarActivity implements View.OnClickListener {
final int MENU_RESET_ID = 1;
final int MENU_QUIT_ID = 2;
EditText editText1;
EditText editText2;
EditText editText3;
EditText editText4;
EditText editText5;
RadioGroup radioGroup;
Button button;
RadioButton radioButton1;
RadioButton radioButton2;
RadioButton radioButton3;
RadioButton radioButton4;
RadioButton radioButton5;
RadioButton radioButton6;
TextView textView;
TextView textView11;
TextView textView12;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText1 = (EditText) findViewById(R.id.editText1);
editText2 = (EditText) findViewById(R.id.editText2);
editText3 = (EditText) findViewById(R.id.editText3);
editText4 = (EditText) findViewById(R.id.editText4);
editText5 = (EditText) findViewById(R.id.editText5);
radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
button = (Button) findViewById(R.id.button);
radioButton1 = (RadioButton) findViewById(R.id.radioButton1);
radioButton2 = (RadioButton) findViewById(R.id.radioButton2);
radioButton3 = (RadioButton) findViewById(R.id.radioButton3);
radioButton4 = (RadioButton) findViewById(R.id.radioButton4);
radioButton5 = (RadioButton) findViewById(R.id.radioButton5);
radioButton6 = (RadioButton) findViewById(R.id.radioButton6);
textView = (TextView) findViewById(R.id.textView);
textView11 = (TextView) findViewById(R.id.textView11);
textView12 = (TextView) findViewById(R.id.textView12);
button.setOnClickListener(this);
editText1.setOnClickListener(this);
}
public void editText1 (View view) {
if(view == editText1) {
editText1.setText("");
do {
radioGroup.setEnabled(false);
}
while (TextUtils.isEmpty(editText1.getText().toString()));
}
}
public void radioButton1 (View view) {
double num1 = 0;
double num6 = 12;
if (TextUtils.isEmpty(editText1.getText().toString())){
radioGroup.clearCheck();
return;}
num1 = Float.parseFloat(editText1.getText().toString());
if (num1 <= num6) {
editText5.setText("5");
}
else {
editText5.setText("7");
}
}
public void radioButton2 (View view) {
if (TextUtils.isEmpty(editText1.getText().toString())){radioGroup.clearCheck(); return;}
editText5.setText("1.3");
}
public void radioButton3 (View view) {
if (TextUtils.isEmpty(editText1.getText().toString())){radioGroup.clearCheck(); return;}
editText5.setText("4.4");
}
public void radioButton4 (View view) {
if (TextUtils.isEmpty(editText1.getText().toString())){radioGroup.clearCheck(); return;}
editText5.setText("4.4");
}
public void radioButton5 (View view) {
if (TextUtils.isEmpty(editText1.getText().toString())){radioGroup.clearCheck(); return;}
editText5.setText("15");
}
public void radioButton6 (View view) {
if (TextUtils.isEmpty(editText1.getText().toString())){radioGroup.clearCheck(); return;}
editText5.setText("3");
}
public void onClick (View v){
double num1 = 0;
float num2 = 0;
float num3 = 0;
float num4 = 0;
float num5 = 0;
double num6 = 12;
double resultmax = 0;
double resulthalf = 0;
if (TextUtils.isEmpty(editText1.getText().toString())
|| TextUtils.isEmpty(editText2.getText().toString())
|| TextUtils.isEmpty(editText3.getText().toString())
|| TextUtils.isEmpty(editText4.getText().toString())
|| TextUtils.isEmpty(editText5.getText().toString())) {
return;}
num1 = Float.parseFloat(editText1.getText().toString());
num2 = Float.parseFloat(editText2.getText().toString());
num3 = Float.parseFloat(editText3.getText().toString());
num4 = Float.parseFloat(editText4.getText().toString());
num5 = Float.parseFloat(editText5.getText().toString());
switch (v.getId()) {
case R.id.button:
resultmax = new BigDecimal(((num2*num5)/num3)/num4).setScale(2, RoundingMode.UP).doubleValue();
resulthalf = new BigDecimal((((num2*num5)/num3)/num4)/2).setScale(2, RoundingMode.UP).doubleValue();
/* switch (radioGroup.getCheckedRadioButtonId()){
case R.id.radioButton1:
if (num1 <= num6) {
Toast.makeText(MainActivity.this, "Внимание доза для ребенка!", Toast.LENGTH_LONG).show();
editText5.setText("5");
}
else {
Toast.makeText(MainActivity.this, "Внимание доза для взрослого!", Toast.LENGTH_LONG).show();
editText5.setText("7");
}
break;
case R.id.radioButton2:
editText5.setText("7");
break;
case R.id.radioButton3:
editText5.setText("7");
break;
case R.id.radioButton4:
editText5.setText("7");
break;
case R.id.radioButton5:
editText5.setText("7");
break;
case R.id.radioButton6:
editText5.setText("7");
break;
case R.id.radioButton7:
editText5.setText("7");
break;
case R.id.radioButton8:
editText5.setText("7");
break;
}*/
break;
default:
break;
}
textView.setText(resultmax + " карпула(-ы)");
textView11.setText(resulthalf + " карпула(-ы)");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
menu.add(0, MENU_RESET_ID, 0, "Очистка");
menu.add(0, MENU_QUIT_ID, 0, "Выйти");
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
// TODO Auto-generated method stub
switch (item.getItemId()) {
case MENU_RESET_ID:
radioGroup.clearCheck();
textView.setText("");
editText1.setText("");
editText2.setText("");
editText3.setText("");
editText4.setText("");
editText5.setText("");
textView11.setText("");
break;
case MENU_QUIT_ID:
finish();
break;
}
return super.onOptionsItemSelected(item);
}
}
[/syntax]
[syntax=xml]<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:id="@+id/linearLayout">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Подсчет"
android:id="@+id/button"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Максимально допустимая доза:"
android:id="@+id/textView9" />
<TextView
android:layout_width="190dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/textView"
android:layout_gravity="right"
android:textColor="#ffff0000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Оптимальная доза, для манипуляций:"
android:id="@+id/textView10" />
<TextView
android:layout_width="190dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/textView11"
android:layout_gravity="right"
android:textColor="#ff008b00" />
</LinearLayout>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/scrollView"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="@+id/linearLayout"
android:layout_alignParentTop="true">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="@+id/linearLayout"
android:weightSum="1">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:weightSum="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Данные пациента:"
android:id="@+id/textView2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Возраст (полных лет):"
android:id="@+id/textView3"
android:textSize="20dp"
android:layout_gravity="left" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="@+id/editText1"
android:textSize="15dp"
android:onClick="editText1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Вес (кг):"
android:id="@+id/textView4"
android:textSize="20dp"
android:layout_gravity="left" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="@+id/editText2"
android:textSize="15dp"
android:nestedScrollingEnabled="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Данные анестетика:"
android:id="@+id/textView5" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Действующее вещество:"
android:id="@+id/textView12"
android:textSize="20dp" />
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/radioGroup">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Артикаин"
android:id="@+id/radioButton1"
android:checked="false"
android:onClick="radioButton1"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Бупивакаин"
android:id="@+id/radioButton2"
android:checked="false"
android:onClick="radioButton2"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Лидокаин"
android:id="@+id/radioButton3"
android:onClick="radioButton3"
android:checked="false" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Мепивакаин"
android:id="@+id/radioButton4"
android:onClick="radioButton4"
android:checked="false" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Прокаин"
android:id="@+id/radioButton5"
android:onClick="radioButton5"
android:checked="false" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ропивакаин"
android:id="@+id/radioButton6"
android:onClick="radioButton6"
android:checked="false" />
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Количесво вещества (мг):"
android:id="@+id/textView6"
android:layout_gravity="left"
android:textSize="20dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="@+id/editText3"
android:layout_weight="0.04"
android:textSize="15dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Объем карпулы (мл):"
android:id="@+id/textView7"
android:textSize="20dp"
android:layout_gravity="left" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="@+id/editText4"
android:layout_weight="0.04"
android:textSize="15dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Максимальная доза (мг/кг):"
android:id="@+id/textView8"
android:textSize="20dp"
android:layout_gravity="left" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="@+id/editText5"
android:layout_weight="0.04"
android:textSize="15dp" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
[/syntax]
Как сделать так, чтобы веденное число при нажатии исчезало? Хотел реализовать через onClick, но что то в голове не вяжется.
Чтобы было так:
Ввел возраст: 50
Ввел вес: 42
Тыкаешь на поле ввода "Возраст" и цифра 50 стиралась.
Как при нажатии на editText исчезал введенный текст?
-
- Сообщения: 22
- Зарегистрирован: 14 авг 2015, 11:46
-
- Сообщения: 22
- Зарегистрирован: 14 авг 2015, 11:46
Re: Как при нажатии на editText исчезал введенный текст?
Если сделать так?
editText1.setFocus().clear();
editText1.setFocus().clear();
Re: Как при нажатии на editText исчезал введенный текст?
свойство hint
Семь раз отмерь - поставь студию.
Эклипс не студия, ошибка вылетит - не исправишь.
Скажи мне кто твой друг, и оба поставили студию.
Студия - свет, а эклипс - тьма.
Эклипс не студия, ошибка вылетит - не исправишь.
Скажи мне кто твой друг, и оба поставили студию.
Студия - свет, а эклипс - тьма.
-
- Сообщения: 22
- Зарегистрирован: 14 авг 2015, 11:46
Re: Как при нажатии на editText исчезал введенный текст?
Hint - это подсказка. А мне надо, чтоб старался ранее введены текст.doter.ua писал(а):свойство hint
Ввели 55. Ткнули на edittext, число 55 стерлось ввели 45.Ткнули на edittext, число 45 стерлось ввели 84.Ткнули на edittext, число 84 стерлось ввели 21.
РЕШЕНИЕ ПРОБЛЕМЫ!
[syntax=java5]editText1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus){
editText1.setText("");
radioGroup.clearCheck();
editText5.setText("");
}
}
});[/syntax]
Re: Как при нажатии на editText исчезал введенный текст?
Для этого делают соответствующую кликабельную икону.
Семь раз отмерь - поставь студию.
Эклипс не студия, ошибка вылетит - не исправишь.
Скажи мне кто твой друг, и оба поставили студию.
Студия - свет, а эклипс - тьма.
Эклипс не студия, ошибка вылетит - не исправишь.
Скажи мне кто твой друг, и оба поставили студию.
Студия - свет, а эклипс - тьма.