0

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

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
    }
};
}
4

3 回答 3

0

假设您的结果页面名为 ResultActivity

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

  startActivity(intent);

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

Daniel 是正确的,您首先需要为保存按钮设置单击侦听器,例如“btn”是 xml 中您的 Button 的 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);

        }
    });
于 2013-01-24T08:10:31.800 回答
0

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

于 2013-01-24T08:23:43.897 回答
0

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

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

于 2013-01-24T08:13:41.887 回答