Код:
package com.jkollss;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.sax.TextElementListener;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;
public class OhmActivity extends Activity {
public static final String APP_PREF = "ohm_settings";
SharedPreferences mSettings;
public static final String[] VOLTS = {"2.9","3.0","3.1","3.2","3.3","3.4","3.5","3.6","3.7","3.8","3.9",
"4.0","4.1","4.2","4.3","4.4","4.5","4.6","4.7","4.8","4.9",
"5.0","5.1","5.2","5.3","5.4","5.5","5.6","5.7","5.8","5.9","6.0"};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText ohm = (EditText) findViewById(R.id.edOhm);
ohm.setText("1");
setVolt(0);
SeekBar volts = (SeekBar) findViewById(R.id.seekVolt);
volts.setMax(VOLTS.length-1);
volts.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
setVolt(progress);
}
});
}
private void setVolt(int index) {
TextView v = (TextView) findViewById(R.id.lblVolts);
v.setText(VOLTS[index]);
calc();
}
private void calc() {
EditText ohm = (EditText) findViewById(R.id.edOhm);
TextView power = (TextView) findViewById(R.id.lblPower);
TextView volts = (TextView) findViewById(R.id.lblVolts);
TextView current = (TextView) findViewById(R.id.lblAmp);
float Resistance = Float.valueOf(ohm.getText().toString());
float Volt = Float.valueOf(volts.getText().toString());
if (Resistance == 0) {
power.setText(R.string.error);
current.setText(R.string.error);
}
float Power = (Volt * Volt) / Resistance;
float Current = Volt / Resistance;
power.setText(String.format("Power: %.2f Watt", Power));
current.setText(String.format("Current: %.2f Amper", Current));
}
}
Код:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/edOhm"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="numberDecimal" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/lblVolts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<SeekBar
android:id="@+id/seekVolt"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/lblAmp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="@+id/lblPower"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>