Android应用程序 – 将用户输入的值值乘以并分成两种forms
我正在尝试创建一个Android应用程序,因为我是一个初学者,我想知道是否有人可以帮我解决这些问题。
将有三个框输入不同的数字,我希望应用程序将第二个值除以第一个,然后将其乘以第三个。 然后在屏幕上显示答案。
该应用确实有一个目的啊哈。
就像(b / a)* c
对于获取输入,需要3个EditText并通过单击获取一个按钮获取结果。
跟着这个
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
在应用程序布局中声明3个EditTexts,以及一个按钮和一个TextView。 给他们独特的id。
以下代码将执行您想要的操作。 这很简单,所以请确保您不仅仅是复制和粘贴,而且您了解它。 我总是觉得从例子中学习更容易,这就是我给出一个例子的原因。 我希望它有所帮助。
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; @Override public 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()); }
}
int answer=(Integer.parse(editTextB.getText()) /Integer.parse(editTextA.getText())*Integer.parse(editTextC.getText()
- 在android应用程序中使用dll库
- 如何在Android中获取Spinner Selected Item
- 你如何从光标中获取类型?
- oauth2 android中的jsonresponse中的invalid_client
- Android java.lang.ClassCastException:android.widget.RelativeLayout无法强制转换为android.widget.EditText
- 如何在Android Studio中调试应用程序时单步执行或跳过代码
- 无法解决方法,为什么?
- 想要使用GoogleMaps – OnMyLocationChangeListener却无法实现吗? 任何其他选择
- 在谷歌地图android中找到两个坐标之间的道路距离