作者在 2016-08-21 18:56:15 发布以下内容
public class MainActivity extends Activity {
private TextView tv1, tv2, tv3, tv4, tv5, tv6, tv7, tv8, tv9, tv10, tv11,
tv12, tv13, tv14;
private EditText et1, et2, et3;
private Button bt1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViews();
bt1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
try {
String metalkind = et1.getText().toString();// get 金质
double weight = Double
.parseDouble(et2.getText().toString());// 原金质重
double realityweight = Double.parseDouble(et3.getText()
.toString());// 回收24K金重
Cleargold1 cg = new Cleargold1();
// tv10.setText(String.valueOf(cg.reNeedWeight(metalkind,
// weight)));// 理论回收
BigDecimal bd1 = new BigDecimal(cg.reNeedWeight(metalkind,
weight));// 四舍5入2位小数
double lilun = bd1.setScale(2, BigDecimal.ROUND_HALF_UP)
.doubleValue();
tv10.setText(String.valueOf(lilun));
// tv11.setText(String.valueOf(cg.reRate(metalkind, weight, realityweight)));//回收率
BigDecimal bd2 = new BigDecimal(cg.reRate(metalkind,
weight, realityweight));
double huishoulu = bd2
.setScale(2, BigDecimal.ROUND_HALF_UP)
.doubleValue();
tv11.setText(String.valueOf(huishoulu));
// tv12.setText(String.valueOf(cg.purity(weight,
// realityweight)));//成色
BigDecimal bd = new BigDecimal(realityweight / weight * 100);// 成色
// //四舍5入2位小数
double chense = bd.setScale(2, BigDecimal.ROUND_HALF_UP)
.doubleValue();
tv12.setText(String.valueOf(chense));
// tv13.setText(String.valueOf(cg.lossValue(metalkind,
// weight, realityweight)));// 损耗值
BigDecimal bd3 = new BigDecimal(cg.lossValue(metalkind,
weight, realityweight));// 四舍5入2位小数
double shenzhi = bd3.setScale(2, BigDecimal.ROUND_HALF_UP)
.doubleValue();
tv13.setText(String.valueOf(shenzhi));
// tv14.setText(String.valueOf(cg.lossRate(metalkind,
// weight, realityweight)));//损耗率
BigDecimal bd4 = new BigDecimal(cg.lossRate(metalkind,
weight, realityweight));// 四舍5入2位小数
double shenhaolu = bd4
.setScale(2, BigDecimal.ROUND_HALF_UP)
.doubleValue();
tv14.setText(String.valueOf(shenhaolu));
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
Toast.makeText(MainActivity.this, "(8k,9k,10k,10kl,14k,14kwi,18k,18kl,22k)", 5000)
.show();
}
}
});
}
private void findViews() {
tv1 = (TextView) findViewById(R.id.textView1);
tv2 = (TextView) findViewById(R.id.textView2);
tv3 = (TextView) findViewById(R.id.textView3);
tv4 = (TextView) findViewById(R.id.textView4);
tv5 = (TextView) findViewById(R.id.textView5);
tv6 = (TextView) findViewById(R.id.textView6);
tv7 = (TextView) findViewById(R.id.textView7);
tv8 = (TextView) findViewById(R.id.textView8);
// tv9 = (TextView) findViewById(R.id.textView9);
tv10 = (TextView) findViewById(R.id.textView10);
tv11 = (TextView) findViewById(R.id.textView11);
tv12 = (TextView) findViewById(R.id.textView12);
tv13 = (TextView) findViewById(R.id.textView13);
tv14 = (TextView) findViewById(R.id.textView14);
et1 = (EditText) findViewById(R.id.editText1);
et2 = (EditText) findViewById(R.id.editText2);
et3 = (EditText) findViewById(R.id.editText3);
bt1 = (Button) findViewById(R.id.button1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
//////////////////////////////////////////////////////////////////
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="金质"
android:textSize="18dp" />
<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="原重(克)"
android:textSize="18dp" />
<TextView
android:id="@+id/textView3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="24K(克)"
android:textSize="18dp" />
</TableRow>
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/editText1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:ems="10"
android:hint="8k...小写k"
android:textSize="18dp" />
<EditText
android:id="@+id/editText2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:ems="10"
android:inputType="numberDecimal"
android:hint="不为0的数"
android:textSize="18dp" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:ems="10"
android:inputType="numberDecimal"
android:hint="实际回收重"
android:textSize="18dp" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="清金计算" />
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="理论24k(克):"
android:textSize="20dp" />
<TextView
android:id="@+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:textSize="20dp" />
</TableRow>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="回 收 率 (%):"
android:textSize="20dp" />
<TextView
android:id="@+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:textSize="20dp" />
</TableRow>
<TableRow>
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="成 色 (%):"
android:textSize="20dp" />
<TextView
android:id="@+id/textView12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:textSize="20dp" />
</TableRow>
<TableRow
android:id="@+id/tableRow5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="损 耗 值 (克):"
android:textSize="20dp" />
<TextView
android:id="@+id/textView13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:textSize="20dp" />
</TableRow>
<TableRow
android:id="@+id/tableRow6"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="损 耗 率 (%):"
android:textSize="20dp" />
<TextView
android:id="@+id/textView14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:textSize="20dp" />
</TableRow>
</TableLayout>
///////////////////////////////////////////////////////////////
public class Cleargold1 {
private String metalkind;
private double weight;//原重
private double realityweight;//回收重
// 应该回收24K的理论值计算
public double reNeedWeight(String metalkind, double weight) {
double i = 0;
try {
if (metalkind.equals("9k")) {
i = weight * 0.377;
} else if (metalkind.equals("10k")) {
i = weight * 0.42;
}else if (metalkind.equals("8k")) {
i = weight * 0.335;
} else if (metalkind.equals("14k")) {
i = weight * 0.588;
} else if (metalkind.equals("14kwi")) {
i = weight * 0.585;
} else if (metalkind.equals("18k")) {
i = weight * 0.753;
} else if (metalkind.equals("18kl")) {
i = weight * 0.756;
} else if (metalkind.equals("10kl")) {
i = weight * 0.422;
} else if (metalkind.equals("22k")) {
i = weight * 0.92;
} else {
System.out.print("其他金质暂不作考虑!");
}
} catch (Exception e) {
e.printStackTrace();
}
return i;
}
// 回收率计算
public double reRate(String metalkind, double weight, double realityweight) {
return realityweight / reNeedWeight(metalkind, weight)*100;
}
// 成色计算
public double purity(double weight,double realityweight ) {
return realityweight / weight * 100;
}
// 损耗计算
public double lossValue(String metalkind, double weight,
double realityweight) {
return (reNeedWeight(metalkind, weight) - realityweight);
}
// 损耗率计算
public double lossRate(String metalkind, double weight, double realityweight) {
return this.lossValue(metalkind, weight, realityweight)
/ this.reNeedWeight(metalkind, weight) * 100;
}
}