Android应用程序 - 将用户输入的值值乘以并分成两种形式

时间:2022-10-11 12:01:48

I'm trying to make an android app, and as I am a total beginner, I am wondering if anyone could help me out with the code.

我正在尝试创建一个Android应用程序,因为我是一个初学者,我想知道是否有人可以帮我解决这些问题。

There will be three boxes to enter different numbers, and I want the app to divide the second value by the first, and then multiply it by the third. And then display the answer on screen.

将有三个框输入不同的数字,我希望应用程序将第二个值除以第一个,然后将其乘以第三个。然后在屏幕上显示答案。

And the app does have a purpose aha.

该应用确实有一个目的啊哈。

So like (b/a)*c

就像(b / a)* c

3 个解决方案

#1


4  

For taking inputs take 3 EditText and take one Button for get Result by Clicking on it.

对于获取输入,需要3个EditText并通过单击获取一个按钮获取结果。

Follow this

public class result extends Activity{ private EditText edit1;private EditText edit2;private EditText edit3;public void onCreate(Bundle savedInstanceState) {    try    {        super.onCreate(savedInstanceState);        setContentView(R.layout.result);        edit1 = (EditText)findViewById(R.id.edit1);        edit2 = (EditText)findViewById(R.id.edit2);        edit3 = (EditText)findViewById(R.id.edit3);        Button click = (Button)findViewById(R.id.btn);        click.setOnClickListener(new OnClickListener() {            public void onClick(View v) {                int a = Integer.parseInt(edit1.getText().toString());                int b = Integer.parseInt(edit2.getText().toString());                int c = Integer.parseInt(edit3.getText().toString());                double result = ((double) a/b)*c;                Toast.makeText(result.this, Double.toString(result),Toast.LENGTH_LONG).show();            }        });    }catch (Exception e) {        e.printStackTrace();    }}}

Result.xml

 <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#fff" ><EditText    android:id="@+id/edit1"   android:layout_width="fill_parent"   android:layout_height="40dp"   android:inputType="text"/><EditText    android:id="@+id/edit2"   android:layout_width="fill_parent"   android:layout_height="40dp"   android:inputType="text"/> <EditText    android:id="@+id/edit3"   android:layout_width="fill_parent"   android:layout_height="40dp"   android:inputType="text"/> <Button   android:id="@+id/btn"   android:layout_width="fill_parent"   android:layout_height="40dp"   android:text="Click"/></LinearLayout>

#2


3  

Declare 3 EditTexts in your applications layout, as well as a button, and a TextView. Give them unique id's.

在应用程序布局中声明3个EditTexts,以及一个按钮和一个TextView。给他们独特的id。

The following code will do what you want. It's very simple, so make sure you don't just copy and paste,and that you understand it. I always find it easier to learn from examples, which is why I'm giving one. I hope it helps.

以下代码将执行您想要的操作。这很简单,所以请确保您不仅仅是复制和粘贴,而且您了解它。我总是觉得从例子中学习更容易,这就是我给出一个例子的原因。我希望它有所帮助。

public class MainActivity extends Activity {    //Declare textviews as fields, so they can be accessed throughout the activity.EditText mEditText1;EditText mEditText2;EditText mEditText3;TextView mTextView;Button mButton;@Overridepublic void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    //Bind the EditText views    mEditText1 = (EditText)findViewById(R.id.editText1);    mEditText2 = (EditText)findViewById(R.id.editText2);    mEditText3 = (EditText)findViewById(R.id.editText3);    mTextView = (TextView)findViewById(R.id.textView1);    mButton =  (Button)findViewById(R.id.calculateButton);    mButton.setOnClickListener(new OnClickListener() {        @Override        public void onClick(View v) {            //When the button is clicked, call the calucate method.            calculate();        }    });}public void calculate(){    //get entered texts from the edittexts,and convert to integers.    Double value1 = Double.parseDouble(mEditText1.getText().toString());    Double value2 = Double.parseDouble(mEditText2.getText().toString());    Double value3 = Double.parseDouble(mEditText3.getText().toString());    //do the calculation    Double calculatedValue = (value2/value1)*value3;    //set the value to the textview, to display on screen.    mTextView.setText(calculatedValue.toString());}

}

#3


0  

int answer=(Integer.parse(editTextB.getText()) /Integer.parse(editTextA.getText())*Integer.parse(editTextC.getText()

#1


4  

For taking inputs take 3 EditText and take one Button for get Result by Clicking on it.

对于获取输入,需要3个EditText并通过单击获取一个按钮获取结果。

Follow this

public class result extends Activity{ private EditText edit1;private EditText edit2;private EditText edit3;public void onCreate(Bundle savedInstanceState) {    try    {        super.onCreate(savedInstanceState);        setContentView(R.layout.result);        edit1 = (EditText)findViewById(R.id.edit1);        edit2 = (EditText)findViewById(R.id.edit2);        edit3 = (EditText)findViewById(R.id.edit3);        Button click = (Button)findViewById(R.id.btn);        click.setOnClickListener(new OnClickListener() {            public void onClick(View v) {                int a = Integer.parseInt(edit1.getText().toString());                int b = Integer.parseInt(edit2.getText().toString());                int c = Integer.parseInt(edit3.getText().toString());                double result = ((double) a/b)*c;                Toast.makeText(result.this, Double.toString(result),Toast.LENGTH_LONG).show();            }        });    }catch (Exception e) {        e.printStackTrace();    }}}

Result.xml

 <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#fff" ><EditText    android:id="@+id/edit1"   android:layout_width="fill_parent"   android:layout_height="40dp"   android:inputType="text"/><EditText    android:id="@+id/edit2"   android:layout_width="fill_parent"   android:layout_height="40dp"   android:inputType="text"/> <EditText    android:id="@+id/edit3"   android:layout_width="fill_parent"   android:layout_height="40dp"   android:inputType="text"/> <Button   android:id="@+id/btn"   android:layout_width="fill_parent"   android:layout_height="40dp"   android:text="Click"/></LinearLayout>

#2


3  

Declare 3 EditTexts in your applications layout, as well as a button, and a TextView. Give them unique id's.

在应用程序布局中声明3个EditTexts,以及一个按钮和一个TextView。给他们独特的id。

The following code will do what you want. It's very simple, so make sure you don't just copy and paste,and that you understand it. I always find it easier to learn from examples, which is why I'm giving one. I hope it helps.

以下代码将执行您想要的操作。这很简单,所以请确保您不仅仅是复制和粘贴,而且您了解它。我总是觉得从例子中学习更容易,这就是我给出一个例子的原因。我希望它有所帮助。

public class MainActivity extends Activity {    //Declare textviews as fields, so they can be accessed throughout the activity.EditText mEditText1;EditText mEditText2;EditText mEditText3;TextView mTextView;Button mButton;@Overridepublic void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    //Bind the EditText views    mEditText1 = (EditText)findViewById(R.id.editText1);    mEditText2 = (EditText)findViewById(R.id.editText2);    mEditText3 = (EditText)findViewById(R.id.editText3);    mTextView = (TextView)findViewById(R.id.textView1);    mButton =  (Button)findViewById(R.id.calculateButton);    mButton.setOnClickListener(new OnClickListener() {        @Override        public void onClick(View v) {            //When the button is clicked, call the calucate method.            calculate();        }    });}public void calculate(){    //get entered texts from the edittexts,and convert to integers.    Double value1 = Double.parseDouble(mEditText1.getText().toString());    Double value2 = Double.parseDouble(mEditText2.getText().toString());    Double value3 = Double.parseDouble(mEditText3.getText().toString());    //do the calculation    Double calculatedValue = (value2/value1)*value3;    //set the value to the textview, to display on screen.    mTextView.setText(calculatedValue.toString());}

}

#3


0  

int answer=(Integer.parse(editTextB.getText()) /Integer.parse(editTextA.getText())*Integer.parse(editTextC.getText()