按保存按钮后自动转到结果页面

时间:2013-01-24 08:06:45

标签: android eclipse

按下保存按钮后如何自动转到结果页面?按下保存按钮后,我的应用程序似乎停留在同一页面上。附在下面的是源代码

DetailForm.Java

public class DetailForm extends Activity {
EditText name = null;
EditText address = null;
EditText telephone = null;
EditText details = null;
NameCardHelper helper = null;
String namecardId = null;
TextView location = null;
LocationManager locMgr = null;
double latitude = 0.0d;
double longitude = 0.0d;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.detail_form);

    locMgr = (LocationManager) getSystemService(LOCATION_SERVICE);

    helper = new NameCardHelper(this);

    name = (EditText) findViewById(R.id.name);
    address = (EditText) findViewById(R.id.address);
    telephone = (EditText) findViewById(R.id.telephone);
    details = (EditText) findViewById(R.id.details);
    location = (TextView) findViewById(R.id.location);

    namecardId = getIntent().getStringExtra(NameCardList.ID_EXTRA);
    if (namecardId != null) {
        load();
    }
}

@Override
public void onPause() {
    save();
    super.onPause();
}

@Override
public void onDestroy() {
    helper.close();
    locMgr.removeUpdates(onLocationChange);
    super.onDestroy();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    new MenuInflater(this).inflate(R.menu.details_option, menu);
    return (super.onCreateOptionsMenu(menu));
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    if (namecardId == null) {
        menu.findItem(R.id.location).setEnabled(false);
        menu.findItem(R.id.map).setEnabled(false);
    }
    return (super.onPrepareOptionsMenu(menu));
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == R.id.location) {

        locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
                onLocationChange);
        return (true);

        } else if (item.getItemId() == R.id.map) {
            Intent i = new Intent(this, NameCardMap.class);
            i.putExtra(NameCardMap.EXTRA_LATITUDE, latitude);
            i.putExtra(NameCardMap.EXTRA_LONGITUDE, longitude);
            i.putExtra(NameCardMap.EXTRA_NAME, name.getText().toString());
            startActivity(i);
            return (true);
        } else if (item.getItemId() == R.id.call) {
             Intent i = new Intent(Intent.ACTION_CALL);

                String p = "tel:" + telephone.getText();
                i.setData(Uri.parse(p));
                startActivity(i);
        } else if (item.getItemId() == R.id.sms) {

            Intent smsIntent = new Intent(Intent.ACTION_SENDTO);
            smsIntent.addCategory(Intent.CATEGORY_DEFAULT);
            smsIntent.setType("vnd.android-dir/mms-sms");
            smsIntent.setData(Uri.parse("sms:" + telephone.getText()));
            startActivity(smsIntent);
        } else if (item.getItemId() == R.id.delete) {

            helper.delete(namecardId);

            startActivity(new Intent(this, NameCardList.class));
            return (true);
        }
    return true;
        }

private boolean isNetworkAvailable() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);

    NetworkInfo info = cm.getActiveNetworkInfo();
    return (info != null);
}

private void load() {
    Cursor c = helper.getById(namecardId);
    c.moveToFirst();

    name.setText(helper.getName(c));
    address.setText(helper.getAddress(c));
    telephone.setText(helper.getTelephone(c));
    details.setText(helper.getDetails(c));

    latitude = helper.getLatitude(c);
    longitude = helper.getLongitude(c);
    location.setText(String.valueOf(latitude) + ", "
            + String.valueOf(longitude));

    c.close();
}

private void save() {
    if (name.getText().toString().length() > 0) {

        if (namecardId == null) {
            helper.insert(name.getText().toString(), address.getText()
                    .toString(), telephone.getText().toString(),
                    details.getText().toString());
        } else {
            helper.update(namecardId, name.getText().toString(), address
                    .getText().toString(), telephone.getText().toString(),
                    details.getText().toString());
        }
    }

}

LocationListener onLocationChange = new LocationListener() {
    public void onLocationChanged(Location fix) {
        helper.updateLocation(namecardId, fix.getLatitude(),
                fix.getLongitude());
        location.setText(String.valueOf(fix.getLatitude()) + ", "
                + String.valueOf(fix.getLongitude()));
        locMgr.removeUpdates(onLocationChange);

        Toast.makeText(DetailForm.this, "Location saved", Toast.LENGTH_LONG)
                .show();
    }

    public void onProviderDisabled(String provider) {
        // required for interface, not used
    }

    public void onProviderEnabled(String provider) {
        // required for interface, not used
    }

    public void onStatusChanged(String provider, int status, Bundle extras) {
        // required for interface, not used
    }
};
}

3 个答案:

答案 0 :(得分:0)

假设您的结果页面名为ResultActivity

  Intent intent = new Intent(this, ResultActivity.class);

  startActivity(intent);

确保结果活动在Manifest :) ...使用保存按钮单击监听器上面的代码

Daniel是正确的,你需要首先为点击监听器设置保存按钮,说“btn”是你的按钮在xml中的id。

  Button btn = (Button) findViewById(R.id.btn);   
  btn .setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          Intent intent = new Intent(this, ResultActivity.class);
          startActivity(intent);

        }
    });

答案 1 :(得分:0)

您的保存按钮在哪里?似乎没有任何参考。

您需要拥有一个公共方法,您可以通过xml中的按钮(通过onClick)与之交谈,或者您需要在源代码中为按钮添加单击回调。您似乎也没有这样做,因此,如果您的XML中有一个按钮,则此时它不会附加到任何操作。

答案 2 :(得分:0)

您是否在xml页面中给出了onClick,即android:Onclick =" save"对于该按钮,它可以工作,否则你可以绑定活动类中的按钮并向其添加动作监听器