Android UI设计 DroidDraw 教程一:Currency Converter
2010-06-04 13:47:00 来源:WEB开发网到这里你就可以在Android中运行你的GUI。它应该像这样:
步骤十六
最后一步是实际的代码货币转换。它不多,你可以用一下代码来查找到你的GUI元素:
this.findViewById(R.id.);
下面是完整CurrentConverter Activity的代码:
package zyf.CurrentConverter;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;
public class CurrentConverter extends Activity
implements OnClickListener {
TextView dollars;
TextView euros;
RadioButton dtoe;
RadioButton etod;
Button convert;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
dollars = (TextView) this.findViewById(R.id.dollars);
euros = (TextView) this.findViewById(R.id.euros);
dtoe = (RadioButton) this.findViewById(R.id.dtoe);
dtoe.setChecked(true);
etod = (RadioButton) this.findViewById(R.id.etod);
convert = (Button) this.findViewById(R.id.convert);
convert.setOnClickListener(this);
}
public void onClick(View v) {
if (dtoe.isChecked()) {
convertDollarsToEuros();
}
if (etod.isChecked()) {
convertEurosToDollars();
}
}
protected void convertDollarsToEuros() {
double val =
Double.parseDouble(dollars.getText().toString());
// in a real app, we'd get this off the 'net
euros.setText(Double.toString(val * 0.67));
}
protected void convertEurosToDollars() {
double val = Double.parseDouble(euros.getText().toString());
// in a real app, we'd get this off the 'net
dollars.setText(Double.toString(val / 0.67));
}
更多精彩
赞助商链接