我是Android环境的初级程序员。我需要你的帮助。
这是我正在尝试做的事情
package com.codeherenow.sicalculator;
import com.codeherenow.sicalculator.R;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;
public class SICalculatorActivity extends Activity {
private TextView PA;
private TextView Interest_Rate;
private TextView Years;
private EditText PA_bar;
private EditText IR_bar;
private SeekBar year_bar;
private Button calculate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sicalculator);
PA = (TextView) findViewById(R.id.PA);
Interest_Rate = (TextView) findViewById(R.id.Interest_Rate);
Years= (TextView) findViewById(R.id.Years);
PA_bar= (EditText) findViewById(R.id.PA_bar);
IR_bar= (EditText) findViewById(R.id.IR_bar);
year_bar=(SeekBar) findViewById(R.id.year_bar);
calculate=(Button) findViewById(R.id.calculate);
calculate.setOnClickListener(new OnClickListener(){
public void onClick(View v){
}
}
}
}
我的xml:
<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=".SICalculatorActivity" >
<TextView
android:id="@+id/Years"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/IR_bar"
android:layout_centerVertical="true"
android:text="2 Year(s)"
android:textSize="20sp" />
<SeekBar
android:id="@+id/year_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/Years"
android:layout_below="@+id/Years"
android:layout_marginTop="21dp" />
<Button
android:id="@+id/calculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/year_bar"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/year_bar"
android:text="Calculate" />
<EditText
android:id="@+id/IR_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/Years"
android:layout_marginBottom="14dp"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/Interest_Rate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/IR_bar"
android:layout_alignLeft="@+id/IR_bar"
android:layout_marginBottom="15dp"
android:text="Interest Rate"
android:textSize="20sp" />
<TextView
android:id="@+id/PA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/PA_bar"
android:layout_alignParentTop="true"
android:layout_marginTop="14dp"
android:text="Principal Amount"
android:textSize="20sp" />
<EditText
android:id="@+id/PA_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/Interest_rate"
android:layout_below="@+id/PA"
android:layout_marginTop="17dp"
android:ems="10"
android:inputType="number" />
</RelativeLayout>
实际上,我需要帮助而不是xml部分。我需要java编码部分的帮助。即使我正在尝试使用onclicklistener,编译器总是在编码页面上显示错误?有人能帮助我吗?
答案 0 :(得分:1)
试试这个:
public class MainActivity extends Activity {
TextView PA;
TextView Interest_Rate;
TextView Years;
EditText PA_bar;
EditText IR_bar;
EditText year_bar;
Button calculate;
TextView t1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PA = (TextView) findViewById(R.id.PA);
Interest_Rate = (TextView) findViewById(R.id.Interest_Rate);
Years = (TextView) findViewById(R.id.Years);
PA_bar = (EditText) findViewById(R.id.PA_bar);
IR_bar = (EditText) findViewById(R.id.IR_bar);
year_bar = (EditText) findViewById(R.id.year_bar);
calculate = (Button) findViewById(R.id.calculate);
t1 = (TextView) findViewById(R.id.t1);
calculate.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
int num1 = Integer.parseInt(PA_bar.getText().toString());
int num2 = Integer.parseInt(IR_bar.getText().toString());
int num = Integer.parseInt(year_bar.getText().toString());
int si = (num1 * num2 * num) / 100;
t1.setText(Integer.toString(si));
}
});
}};
答案 1 :(得分:0)
您的代码中存在语法错误。您缺少右括号和分号。
calculate.setOnClickListener(new OnClickListener() {
public void onClick(View v){
}
}); //<--- Change is here.