1

我为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 事件)时,它什么也不做。我拥有的文本视图根本没有更新。

我什至在 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>

有什么问题?我能做些什么来确保我做的一切都是正确的?

ps 我不知道这是否重要,但我已经通过 MainActivity 上的按钮单击事件以这种方式打开了此活动 1:

setContentView(R.layout.activity_step1);

显然,该按钮的事件工作得很好。

谢谢,

4

2 回答 2

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);
        }
    });
于 2012-07-19T07:02:25.250 回答
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()lifepathTextView并且,它似乎工作正常。见下面的屏幕 -

截屏

于 2012-07-19T07:08:52.090 回答