好吧,我还不能发布图片:/
基本上,我有一个带有三个edittext字段,三个复选框,一个计算按钮和一个文本字段的应用程序来输出答案。我希望程序扫描以查看缺少哪个值,以便从中知道它应该执行哪个计算。另一种方法是让用户检查其中一个复选框,然后程序只运行相应复选框的计算。 例如,如果用户输入Q = 10,T = 2,并选中I的复选框,程序将进行数学运算并显示I = 20? 这很有道理 我在伪代码中使用它看起来像 “如果选中复选框1并输入了Q和T的值,则计算I的值并显示在答案字段中”但是我将在那里添加一些错误检查和验证方法。 如果你需要xml文件和java文件那么请问,我只是在寻找一种方法,我怎么能做到这一点,我不希望你们都浪费你的空闲时间为我编写一个完整的程序。 欢呼男人和女孩
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background1"
android:gravity="fill"
android:orientation="vertical"
android:textColor="#ffffff"
tools:context=".CurrentPage" >
<!--
The primary full-screen view. This can be replaced with whatever view
is needed to present your content, e.g. VideoView, SurfaceView,
TextureView, etc.
-->
<!--
This FrameLayout insets its children based on system windows using
android:fitsSystemWindows.
-->
<TextView
android:id="@+id/Current_hint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_marginTop="33dp"
android:text="Welcome to the Current Calculation page."Hint - Use the check boxes to find the values that are missing "
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#ffffff"
tools:ignore="HardcodedText" />
<TextView
android:id="@+id/Equation_letter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/current_calculate"
android:layout_marginTop="37dp"
android:layout_toLeftOf="@+id/current_calculate"
android:text=" HELLO "
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
tools:ignore="HardcodedText" />
<TextView
android:id="@+id/Distances_answer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/Equation_letter"
android:layout_alignBottom="@+id/Equation_letter"
android:layout_alignLeft="@+id/current_calculate"
android:layout_alignParentRight="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff" />
<TextView
android:id="@+id/t"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/Q"
android:layout_alignRight="@+id/Q"
android:layout_below="@+id/Q"
android:layout_marginTop="36dp"
android:gravity="right|top"
android:text="t ="
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
tools:ignore="HardcodedText" />
<CheckBox
android:id="@+id/custom3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/t"
android:layout_alignLeft="@+id/custom2"
android:background="@drawable/check_box_new"
android:button="@drawable/check_box_new" />
<CheckBox
android:id="@+id/custom1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/number_input_3"
android:layout_alignLeft="@+id/custom3"
android:background="@drawable/check_box_new"
android:button="@drawable/check_box_new" />
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/Current_hint"
android:layout_alignRight="@+id/Current_hint"
android:layout_below="@+id/Equation_letter"
android:layout_marginTop="25dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/CurrentmainHelp"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1.31"
android:text="HELP! This equation can be used to calculate the current of an object in amps. The correct equation to discover amps is I=Q/t. Where I = current, Q = charge flowing past a point in the circuit, and t = time taken for the charge to flow. Please note, Q is measure in coulombs and t is measured in seconds, with I being measured in amperes.
Still need more help? "
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="18sp"
tools:ignore="HardcodedText" />
<Button
android:id="@+id/Yes_Please"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="36dp"
android:layout_weight="1.31"
android:onClick="CurrentHelp"
android:text="Yes Please"
android:textColor="#ffffff" />
</LinearLayout>
</ScrollView>
<Button
android:id="@+id/current_calculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/Calculate_Current"
android:textColor="#ffffff"
/>
<EditText
android:id="@+id/number_input_2"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_above="@+id/t"
android:layout_alignLeft="@+id/number_input_1"
android:layout_alignRight="@+id/number_input_1"
android:ems="10"
android:focusable="true"
android:inputType="numberDecimal"
android:textColor="#ffffff" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/number_input_3"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/t"
android:layout_alignLeft="@+id/number_input_2"
android:layout_alignRight="@+id/number_input_2"
android:ems="10"
android:focusable="true"
android:inputType="numberDecimal"
android:textColor="#ffffff" />
<CheckBox
android:id="@+id/custom2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/number_input_1"
android:layout_marginLeft="45dp"
android:layout_toRightOf="@+id/number_input_1"
android:background="@drawable/check_box_new"
android:button="@drawable/check_box_new" />
<EditText
android:id="@+id/number_input_1"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_above="@+id/Q"
android:layout_alignRight="@+id/current_calculate"
android:ems="10"
android:focusable="false"
android:inputType="numberDecimal"
android:singleLine="true"
android:textColor="#ffffff" />
<TextView
android:id="@+id/Q"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/Equation_letter"
android:layout_below="@+id/I"
android:layout_marginTop="36dp"
android:gravity="right|top"
android:text="Q ="
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
tools:ignore="HardcodedText" />
<TextView
android:id="@+id/I"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/Q"
android:layout_alignRight="@+id/Q"
android:layout_below="@+id/Current_hint"
android:layout_marginTop="65dp"
android:gravity="right|top"
android:text="I ="
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
tools:ignore="HardcodedText" />
好吧,那是xml,到目前为止这是java
package com.kieran.whetherfieldphysicscalculator;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.support.v4.app.NavUtils;
// i=q/t this will be the first calculation on the list
public class Current extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_current);
// Show the Up button in the action bar.
setupActionBar();
Button calc1 = (Button)findViewById(R.id.current_calculate);
calc1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText Charge1 = (EditText)findViewById(R.id.number_input_2);
EditText Time1 = (EditText)findViewById(R.id.number_input_3);
TextView Distances_answer = (TextView)findViewById(R.id.Distances_answer);
double charge = Double.parseDouble(Charge1.getText().toString());
double time = Double.parseDouble(Time1.getText().toString());
//Time is a class in Java
Distances_answer.setText("" +charge*time);
}
});
}
public void CurrentHelp(View view) {
// Do something in response to button
Intent intent = new Intent(this, CurrentHelp.class);
startActivity(intent);
}
/**
* Set up the {@link android.app.ActionBar}.
*/
private void setupActionBar() {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
我在问是否有更简单的方法来合成程序来查找值,而不是必须编写每个单独的段? 欢呼声
答案 0 :(得分:0)
在您的情况下,我猜您希望用户只选中一个复选框,并且只应向用户显示与该复选框对应的答案。
因此,您需要在复选框上设置OnClickListener,以便选择相应的操作
阅读here,了解如何在复选框上设置监听器并使用它。特别要查找addListenerOnChkIos()方法,因为它具有您想要的代码
还要在计算按钮上设置另一个OnClickListener,并在该检查中选中哪个复选框,具体取决于您将执行操作并更新textview。