Java 在Android中,我如何实现使用按键增加双倍值的逻辑
我已经创建了按钮Java 在Android中,我如何实现使用按键增加双倍值的逻辑,java,android,button,if-statement,onclick,Java,Android,Button,If Statement,Onclick,我已经创建了按钮 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" androi
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.paulk.tipcalc.TipCalc" >
<EditText
android:id="@+id/BillEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/billTextView"
android:layout_alignBottom="@+id/billTextView"
android:layout_toEndOf="@+id/billTextView"
android:layout_toRightOf="@+id/billTextView"
android:ems="5"
android:inputType="numberDecimal"
android:text="@string/bill_edit_text" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/billTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="19dp"
android:layout_marginLeft="19dp"
android:layout_marginTop="20dp"
android:text="@string/bill_text_view" />
<TextView
android:id="@+id/tipTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/BillEditText"
android:layout_marginStart="34dp"
android:layout_marginLeft="34dp"
android:layout_toEndOf="@+id/billTextView"
android:layout_toRightOf="@+id/BillEditText"
android:text="@string/tip_text_view" />
<Button
android:id="@+id/incrementButton"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignEnd="@+id/tipEditText"
android:layout_alignRight="@+id/tipEditText"
android:layout_alignTop="@+id/tipTextView"
android:onClick="increaaseTip"
android:text="@string/increment"
android:textSize="12sp" />
<TextView
android:id="@+id/totalTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tipEditText"
android:layout_toEndOf="@+id/billTextView"
android:layout_toRightOf="@+id/billTextView"
android:text="@string/total_text_view" />
<EditText
android:id="@+id/totalEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/totalTextView"
android:layout_marginTop="19dp"
android:layout_toStartOf="@+id/tipTextView"
android:layout_toLeftOf="@+id/tipTextView"
android:ems="5"
android:inputType="numberDecimal"
android:text="@string/total_edit_text" />
<EditText
android:id="@+id/tipEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/incrementButton"
android:layout_marginTop="20dp"
android:layout_toEndOf="@+id/billTextView"
android:layout_toRightOf="@+id/tipTextView"
android:ems="4"
android:inputType="numberDecimal"
android:text="@string/tip_edit_text" />
<Button
android:id="@+id/decrementButton"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_above="@+id/tipEditText"
android:layout_marginEnd="12dp"
android:layout_marginRight="12dp"
android:layout_toLeftOf="@+id/incrementButton"
android:layout_toStartOf="@+id/incrementButton"
android:text="@string/decrement"
android:textSize="12sp"
android:onClick="DecreaseTip"/>
<SeekBar
android:id="@+id/changeTipSeekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/billTextView"
android:layout_alignRight="@+id/BillEditText"
android:layout_alignTop="@+id/tipEditText"
android:progress="15" />
<TextView
android:id="@+id/changeTipTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/changeTipSeekBar"
android:layout_alignLeft="@+id/changeTipSeekBar"
android:text="@string/change_tip_text_view" />
</RelativeLayout>
我已经把它注释掉了,因为我不确定这是创建按钮逻辑的最佳方式
/*public void onClick(View view){
if(increment.){
tipAmount =
tipAmount = tipAmount * 0.01;
tipAmount + 0.01;
//double tipAmount = Double.parseDouble(tipAmountET.getText().toString());
tipAmountET.setText(String.format("%.02f", tipAmount));
}else if(view == decrement){
}
}*/
private void updateTipandTotalAmount(){
//converts the double value into a string value
double tipAmount = Double.parseDouble(tipAmountET.getText().toString());
//how the total amount is calculated
double totalAmount = billBeforeTip + (billBeforeTip * tipAmount);
//formating the total amount
totalAmountET.setText(String.format("%.02f", totalAmount));
}
protected void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
outState.putDouble(BILL_WITHOUT_TIP, billBeforeTip);
outState.putDouble(CURRENT_TIP, tipAmount);
outState.putDouble(TOTAL_BILL, totalAmount);
}
private OnSeekBarChangeListener tipSeekBarChangeListener = new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
tipAmount = (tipSeekBar.getProgress()*.01);
tipAmountET.setText(String.format("%.02f", tipAmount));
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.tip_calc, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}通过设置按钮的侦听器,可以使用按钮增加或减少计数器
/*public void onClick(View view){
if(increment.){
tipAmount =
tipAmount = tipAmount * 0.01;
tipAmount + 0.01;
//double tipAmount = Double.parseDouble(tipAmountET.getText().toString());
tipAmountET.setText(String.format("%.02f", tipAmount));
}else if(view == decrement){
}
}*/
private void updateTipandTotalAmount(){
//converts the double value into a string value
double tipAmount = Double.parseDouble(tipAmountET.getText().toString());
//how the total amount is calculated
double totalAmount = billBeforeTip + (billBeforeTip * tipAmount);
//formating the total amount
totalAmountET.setText(String.format("%.02f", totalAmount));
}
protected void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
outState.putDouble(BILL_WITHOUT_TIP, billBeforeTip);
outState.putDouble(CURRENT_TIP, tipAmount);
outState.putDouble(TOTAL_BILL, totalAmount);
}
private OnSeekBarChangeListener tipSeekBarChangeListener = new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
tipAmount = (tipSeekBar.getProgress()*.01);
tipAmountET.setText(String.format("%.02f", tipAmount));
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.tip_calc, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
increment.setOnclickListener()
现在重写onClick()方法,检查单击了哪个按钮,并编写所需的代码
@override
public void onClick(View view){
if(view.getId==R.id.incrementButton){
tipAmount =
tipAmount = tipAmount * 0.01;
tipAmount + 0.01;
//double tipAmount = Double.parseDouble(tipAmountET.getText().toString());
tipAmountET.setText(String.format("%.02f", tipAmount));
}
if(view.getId==R.id.decrementButton){
//decrement code.
}
}