【问题标题】:Event handling from Dialog box对话框中的事件处理
【发布时间】:2012-04-17 11:51:23
【问题描述】:

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

如果我想访问 Imageview,还有一件事;在扩展对话框的类中,从打开对话框的活动中,我该怎么做呢? 我正在尝试执行以下操作: 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);
}

图像处理选项.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;
    }
}
}

【问题讨论】:

    标签: android


    【解决方案1】:

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

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

    【讨论】:

    • 对不起,我怎么能实现第二个,我认为这是不可能的。第一次我也试过了。
    • 那么你的 ImageView 是 null 因为它只是在方法上实例化,如果方法不起作用,图像将包含 null。
    • 顺便问一下,对话框中的 ImageView 是 xml 吗?如果没有,请尝试context.findViewById(R.id.yourimageview)
    • No ImageView 来自 Activity 并且上面的代码不正确我收到错误:方法 findViewById(int) 未定义类型 Context
    • @AnshumanJaiswal 尝试使用PhotoHandler.this 而不是只使用this。如果这不起作用,则可能与 ASyncTask 有关。
    【解决方案2】:

    我在我的代码中实现 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。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多