我在android中为活动编写了以下代码:
import java.util.Date;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.support.v4.app.NavUtils;
public class Step1Activity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_step1);
Button calculateButton = (Button) findViewById(R.id.buttonCalculate);
calculateButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
EditText fullname= (EditText)findViewById(R.id.editText1); // !!Breakpoint!!
DatePicker birthdate= (DatePicker)findViewById(R.id.datePickerDateofBirth);
int test=birthdate.getYear();
TextView result=(TextView)findViewById(R.id.textView2);
result.setText(new Integer( lifepath).toString()+" "+new Integer(test).toString());
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_step1, menu);
return true;
}
}
然而,当我点击calculateButton(它有onclick事件)时,它什么也没做。 我的textview根本没有更新。
我甚至在onClick的第一行(在eclipse中)放了一个断点,它永远不会停在那里。
这是activity1的布局:
<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" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Your full name:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="14dp"
android:ems="10"
android:inputType="textPersonName" />
<TextView
android:id="@+id/TextView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_margin="5dip"
android:layout_below="@+id/editText1"
android:text="Your date of birth:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<DatePicker
android:id="@+id/datePickerDateofBirth"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/buttonCalculate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Calculate Now" />
<TextView
android:id="@+id/textView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
</RelativeLayout>
可能有什么不对? 我该怎么做才能确保我做的一切正确?
P.S。我不知道这可能是重要的,但我已经通过这种方式从MainActivity上的按钮点击事件打开了这个activity1:
setContentView(R.layout.activity_step1);
显然,该按钮的事件运作正常。
谢谢,
答案 0 :(得分:5)
你导入了这个
import android.view.View.OnClickListener;
尝试一次
将这4行放在setOnClickListener
之外EditText fullname= (EditText)findViewById(R.id.editText1); // !!Breakpoint!!
DatePicker birthdate= (DatePicker)findViewById(R.id.datePickerDateofBirth);
int test=birthdate.getYear();
final TextView result=(TextView)findViewById(R.id.textView2);
Button calculateButton = (Button) findViewById(R.id.buttonCalculate);
calculateButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
result.setText(new Integer( lifepath).toString()+" "+new Integer(test).toString());
}
}
请参阅我的代码
btnSignUp = (Button) findViewById(R.id.btnSignUp);
btnSignIn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(WepwareActivity.this, SignInActivity.class);
startActivity(i);
}
});
答案 1 :(得分:1)
您可以尝试这种方式 -
1)View.OnClickListener
Button calculateButton = (Button) findViewById(R.id.buttonCalculate);
calculateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText fullname= (EditText)findViewById(R.id.editText1); // !!Breakpoint!!
DatePicker birthdate= (DatePicker)findViewById(R.id.datePickerDateofBirth);
int test=birthdate.getYear();
TextView result=(TextView)findViewById(R.id.textView2);
result.setText(new Integer( lifepath).toString()+" "+new Integer(test).toString());
}
});
2)OnClickListener
Button calculateButton = (Button) findViewById(R.id.buttonCalculate);
calculateButton.setOnClickListener(this);
// outside of oncreate. This will automatically created when you're let implement the onclick listener to your activity
@Override
public void onClick(View v)
{
switch(v.getId())
{
case R.id.buttonCalculate:
EditText fullname= (EditText)findViewById(R.id.editText1); // !!Breakpoint!!
DatePicker birthdate= (DatePicker)findViewById(R.id.datePickerDateofBirth);
int test=birthdate.getYear();
TextView result=(TextView)findViewById(R.id.textView2);
result.setText(new Integer( lifepath).toString()+" "+new Integer(test).toString());
break;
}
}
<强>更新强>
package dummy.usage;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
public class DummyUsageActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.third);
Button calculateButton = (Button) findViewById(R.id.buttonCalculate);
calculateButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText fullname= (EditText)findViewById(R.id.editText1); // !!Breakpoint!!
DatePicker birthdate= (DatePicker)findViewById(R.id.datePickerDateofBirth);
int test=birthdate.getYear();
TextView result=(TextView)findViewById(R.id.textView2);
String lifepath = fullname.getText().toString();
result.setText(lifepath+" "+new Integer(test).toString());
}
});
}
}
从您的代码中。首先我得到NumberFormatException
我刚刚删除了new Integer()
lifepath
的{{1}}而且,似乎工作正常。见下面的屏幕 -