“对话框”中的事件处理

时间:2012-04-17 11:51:23

标签: android

我在点击按钮时创建一个对话框。为此,我创建了一个扩展Dialog的类。我创建了一个XML文件,其中包含此XML文件中的选项(所有文本视图都是文本视图[7 text view]),我为每个文本视图定义了一个标记android:onClick =“imageProcessingHandler”。当我从活动中单击按钮时,它会打开一个对话框,没问题。现在,当我点击任何textview时,我收到一个错误,即函数未定义。我在类中将此函数定义为扩展Dialog的public。有人可以帮忙吗?

如果我想访问Imageview还有一件事;在扩展Dialog框的对话框的类中,所以我怎么能这样做? 我正在尝试执行以下操作: ImageView imgView =(ImageView)findViewById(R.id.fullphoto);但在这种情况下,imgView包含空值:(

代码:

点击按钮上的活动“MyActivity”我正在创建一个对象:

ShowOptionsInDialog displayDialog = new ShowOptionsInDialog(this, passedData);
displayDialog.show();

ShowOptionsInDialog类:

public class ShowOptionsInDialog extends Dialog{

private Context context;

public ShowOptionsInDialog(Context context, ArrayList<String> data) {
    super(context);
    this.context = (Context) context;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.imageprocessingoptions);
    setTitle("Try options");
}

public void imageProcessingHandler(View view){
ImageView photo = (ImageView) findViewById(R.id.fullphoto);
}

imageprocessingoptions.XML

                    

<TextView
    android:id="@+id/invertColorText"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:cursorVisible="false"
    android:paddingLeft="10dp"
    android:paddingBottom="5dp"
    android:gravity="bottom"
    android:text="Invert Color"
    android:textSize="15dp"
    android:clickable="true"
    android:onClick="imageProcessingHandler" />

活动代码:

public class PhotoHandler extends Activity {
private Bundle intentExtra;
private String photoID;

private CommomObject _commonObject;
private Facebook facebook;

private ImageView photo;
private DownloadImageTask downlaodImageTask;

private ProgressDialog progDialog;

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

    intentExtra = getIntent().getExtras();
    photoID = intentExtra.getString("PHOTO_ID");

    displayPhoto();
}

private void displayPhoto() {
    _commonObject = CommomObject.getInstance();
    facebook = _commonObject.getFacebookInstance();
    String imageURL = "https://graph.facebook.com/"+photoID+"/picture?type=normal&access_token="+facebook.getAccessToken();
    photo = (ImageView) findViewById(R.id.fullphoto);
    photo.setTag(imageURL);
    photo.setScaleType(ScaleType.FIT_CENTER);
    downlaodImageTask = new DownloadImageTask();
    progDialog = new ProgressDialog(this);
    progDialog.setMessage("Fetching photo...");
    progDialog.show();
    downlaodImageTask.execute(photo);
}

public class DownloadImageTask extends AsyncTask<ImageView, Void, Bitmap>{
    ImageView imageView = null;

    @Override
    protected Bitmap doInBackground(ImageView... imageViews){
        this.imageView = imageViews[0];
        return downloadImage((String)imageView.getTag());
    }

    @Override
    protected void onPostExecute(Bitmap result) {
        imageView.setImageBitmap(result);
    }

    private Bitmap downloadImage(String url) {

        Bitmap photo = null;

        try {

            URL u = new URL(url);
            URLConnection c = u.openConnection();
            c.connect();
            BufferedInputStream stream = new BufferedInputStream(c.getInputStream());
            photo = BitmapFactory.decodeStream(stream);
            stream.close();

        } catch (MalformedURLException e) {
            Log.e("PhotoHandler", "malformed url: " + url);
        } catch (IOException e) {
            Log.e("PhotoHandler", "An error has occurred downloading the image: " + url);
        }
        progDialog.dismiss();
        return photo;
    }
}

public void footerHandler(View view){
    switch(view.getId()){
    case R.id.useButton:
        break;
    case R.id.saveButton:
        break;
    case R.id.shareButton:
        break;
    case R.id.photoEffect:
        ArrayList<String> passedData = new ArrayList<String>();
        ShowOptionsInDialog displayDialog = new ShowOptionsInDialog(this, passedData);
        displayDialog.show();
        break;
    }
}

public void imageProcessingHandler(View view){
    switch(view.getId()){
    case R.id.invertColorText:
        Bitmap newBitmap = ImageProcessing.doInvert(photo.getDrawingCache());
        photo.setImageBitmap(newBitmap);
        break;
    case R.id.greyScaleText:
        break;
    case R.id.gammaCorrectionText:
        break;
    case R.id.sepiaEffectText:
        break;
    case R.id.embossingEffectText:
        break;
    case R.id.reflectionEffectText:
        break;
    case R.id.contrastText:
        break;
    }
}
}

2 个答案:

答案 0 :(得分:0)

1 - 您需要将public void abcFunction(View view)放在Dialog类中。

2 - 使用yourdialog.findViewById(R.id.fullphoto);

答案 1 :(得分:0)

我在我的代码中实现onCliclk监听器:

TextView invert = (TextView) findViewById(R.id.invertColorText);
invert.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
 // do your work here
});

为了在Dialog上获取ImageView,我在Dailog中从活动中传递了它。