我可以将onActivityResult()的uri值传递给onClick()吗?

时间:2010-07-27 19:37:16

标签: android

public void onCreate(Bundle savedInstanceState)
{
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  mSurfaceView01 = (SurfaceView) findViewById(R.id.mSurfaceView1); 
  mSurfaceHolder01 = mSurfaceView01.getHolder();
  mSurfaceHolder01.addCallback(EX10_04.this);

  mButton02 = (Button)findViewById(R.id.buttonObj);
  mButton02.setOnClickListener( new Button.OnClickListener(){
    public void onClick(View arg0) {
        Intent intent = new Intent( Intent.ACTION_GET_CONTENT );
        intent.setType("audio/*");          
        Intent destIntent = Intent.createChooser( intent, "select audio" ); //pick up an audio file
        startActivityForResult( destIntent, 0 );
    }
  });
 }

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if ( resultCode == RESULT_OK )
{

    Uri uri = data.getData();
    if( uri != null )
    {  
      DrawO();//It will draw a circle
    }
    else
    {
      e.printStackTrace();
    }
}

圈子没有显示..... 但是当我把DrawO()放在

旁边时
  mSurfaceHolder01.addCallback(EX10_04.this);

画一个圆圈!

如果我想在onActivityResult事件中绘制一个圆圈

我做了什么?

或者我可以将uri传递给onClick函数吗?

public void DrawO()
{

Canvas mCanvas01 = mSurfaceHolder01.lockCanvas(null);
mCanvas01.drawColor(getResources().getColor(R.drawable.white)); 
Paint mPaint01 = new Paint();
mPaint01.setStyle(Paint.Style.FILL);
mPaint01.setColor(getResources().getColor(R.drawable.red));
mPaint01.setStrokeWidth(1.0F);
........
.......
}

1 个答案:

答案 0 :(得分:0)

1)未绘制圆,因为尚未创建曲面。尝试这样的事情:

    boolean needDrawing = false;

    public void onResume() {
       if (needDrawing) {
           Draw0();
           needDrawing = false;
       }
       ...
    }

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if ( resultCode == RESULT_OK )
{

    Uri uri = data.getData();
    if( uri != null )
    {  
     needDrawing = true;//It asks to draw a circle
    }
    else
    {
      e.printStackTrace();
    }
}

2)要传递URI,只需声明一个字段URI并在onActivityResult中指定它,并在onClick()中检查是否为null,使用它并使其无效