Добрый день!
Решил написать программу для решения квадратных уравнений.
Фаил equation.xml написал, но с написание Activity_four.java возникли проблемы.
Код equation.xml
Код: Выделить всё
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/background_color"
android:paddingLeft="20dip"
android:paddingRight="20dip" >
<LinearLayout
android:id="@+id/equation"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingBottom="5dip"
android:paddingTop="20dip" >
<EditText
android:id="@+id/a"
android:layout_width="65dip"
android:layout_height="wrap_content"
android:hint="@string/a"
android:inputType="numberSigned"
android:textSize="25sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/xqudratic_text"
android:textColor="@color/equal_text"
android:textSize="25sp" />
<EditText
android:id="@+id/b"
android:layout_width="65dip"
android:layout_height="wrap_content"
android:hint="@string/b"
android:inputType="numberSigned"
android:textSize="25sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/x_text"
android:textColor="@color/equal_text"
android:textSize="25sp" />
<EditText
android:id="@+id/c"
android:hint="@string/c"
android:inputType="numberSigned"
android:layout_width="65dip"
android:layout_height="wrap_content"
android:textSize="25sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/equal_text"
android:textColor="@color/equal_text"
android:textSize="25sp" />
</LinearLayout>
<Button
android:id="@+id/D"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/equation"
android:layout_below="@+id/equation"
android:text="дискриминант" />
<Button
android:id="@+id/roots"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/equation"
android:layout_toRightOf="@+id/D"
android:text="корни" />
<Button
android:id="@+id/solve"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/D"
android:layout_below="@+id/D"
android:text="@string/solve_button"
android:textSize="20sp" />
<Button
android:id="@+id/clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/resultField"
android:layout_alignRight="@+id/resultField"
android:layout_below="@+id/solve"
android:text="очистить"
android:textSize="20sp" />
<TextView
android:id="@+id/resultField"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/solve"
android:layout_below="@+id/button1"
android:layout_marginTop="11dp"
android:paddingTop="18dip"
android:textColor="@color/equal_text"
android:textSize="15sp" />
</RelativeLayout>
Код Activity_four.java
Код: Выделить всё
package ru.experiment;
import ru.gen.equation.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Activity_four extends Activity
implements View.OnClickListener, View.OnKeyListener
{
EditText a;
EditText b;
EditText c;
double d;
double x1;
double x2;
Button roots;
Button D;
Button solve;
Button clear;
TextView resultField;
String operator = "";
String operator2 = "";
float num1 = 0;
float num2 = 0;
double d3 = 0;
protected void alertBox(String paramString1, String paramString2)
{
new AlertDialog.Builder(this).setMessage(paramString2).setTitle(paramString1).setCancelable(true).setNeutralButton(17039370, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface paramAnonymousDialogInterface, int paramAnonymousInt)
{
}
}).show();
}
public void onClick(View paramView)
{
if (TextUtils.isEmpty(a.getText().toString())
|| TextUtils.isEmpty(b.getText().toString())|| TextUtils.isEmpty(c.getText().toString())) {
return;
}
String num1 = this.a.getText().toString();
String num2 = this.b.getText().toString();
String num3 = this.c.getText().toString();
double d1;
double d2;
d1 = Double.parseDouble(num1);
d2 = Double.parseDouble(num2);
d3 = Double.parseDouble(num3);
switch (paramView.getId())
{
case R.id.tvResult:
default:
return;
case R.id.D:
if(d1!=0){
d = Math.pow(d2, 2) - 4 * d1 * d2; }
else if(d1==0){Activity_four.this.alertBox("Ошибка!", "Параметр a не должен равнятся нулю.");}
return;
case R.id.roots:
if(d1!=0){
d = Math.pow(d2, 2) - 4 * d1 * d2;
if (d >= 0) {
x1 = ((-d2 + Math.sqrt(d))/2*d1);
x2 = ((-d2 - Math.sqrt(d))/2*d1);
}
}
else if(d1==0){Activity_four.this.alertBox("Ошибка!", "Параметр a не должен равнятся нулю.");}
}
return;
case R.id.clear:
return;
case R.id.solve:
}
public void onCreate(Bundle paramBundle)
{
super.onCreate(paramBundle);
setContentView(R.layout.equation);
this.a = ((EditText)findViewById(R.id.a));
this.b = ((EditText)findViewById(R.id.b));
this.b = ((EditText)findViewById(R.id.c));
this.resultField = ((TextView)findViewById(R.id.resultField));
this.D = ((Button)findViewById(R.id.D));
this.roots = ((Button)findViewById(R.id.roots));
this.solve = ((Button)findViewById(R.id.solve));
this.clear = ((Button)findViewById(R.id.clear));
D.setOnClickListener(this);
roots.setOnClickListener(this);
solve.setOnClickListener(this);
clear.setOnClickListener(this);
a.setOnKeyListener(this);
b.setOnKeyListener(this);
c.setOnKeyListener(this);
D.setOnKeyListener(this);
roots.setOnKeyListener(this);
}
@Override
public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
// TODO Автоматически созданная заглушка метода
return false;
}
}
Помогите пожалуйста, дополнить код Activity, необходимо чтобы:
1) при нажатии на одну из кнопок(D или roots) считал результат но не выводился.
2) при нажатии на кнопку solve полученный результат выводился(в зависимости от того какая кнопка нажата(D или roots)
если D то:"D=(значение дискриминанта)",
если roots то:"x1=(значение первого корня)" и "x2=(значение второго корня)")
3)при нажатии на кнопку clear все поля(a, b, c, resultField ) и все подсчеты(X1, X2, D) очищались
4)если не нажата одна из кнопок(D или roots) то при нажатии на кнопку solve ничего не выводилось
Так же у меня возникали лаги:
1)при первом подсчете результат выводился только при повторном нажатии кнопки solve.
Кто знает как все сделать прошу помочь!!!!
Заранее спасибо!!!
P.S. Любая критика кода приветствуется.