关于android中的活动

时间:2010-09-07 11:08:49

标签: android

请给我解决方案

public class Calc extends Activity implements OnClickListener 
{
       private Button clr;            
       clr=(Button)findViewById(R.id.Button01);
}

public void onClick(View v)
{
   case R.id.Button01:       
       tv1.setText("REF");
       if( ref_flg==true)
       {
          final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
          alertDialog.setTitle("Reset...");
          alertDialog.setMessage("REF was already entered");
          alertDialog.setButton2("OK", new DialogInterface.OnClickListener()
          {
              public void onClick(DialogInterface dialog, int which) {
                  // here you can add functions
                  dialog.dismiss();
              }
          });
          alertDialog.setIcon(R.drawable.icon);
          alertDialog.show();
       } 
}

所以我的问题是我如何在另一个活动中编写case button1中的代码并从中获取结果。

2 个答案:

答案 0 :(得分:2)

startActivityForResult示例:

Intent intent = new Intent(this, com.example.app.YOURCLASS);
startActivityForResult(intent);

倾听结果:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // See which child activity is calling us back.
    switch (resultCode) {
        case MY_RESULT_CODE:
            // Do something
        default:
            break;
    }
}

从其他活动发送结果:

private OnClickListener myListener = new OnClickListener() {
    public void onClick(View v) {
        Bundle data= new Bundle();
        data.putString("data",""); 
        setResult(RESULT_OK, "DataName", data);
        finish();
    }
}

答案 1 :(得分:0)