Android - 多次单击图像以打开对话框

时间:2015-03-05 00:39:01

标签: android image click imageview

我正在寻找一个代码,让我点击一次imageView 3次来打开一个对话框。那会非常有帮助,谢谢!

这是我的代码:

 package natanrosenfeld.texteditor;
    import android.content.Intent;
    import android.support.v7.app.ActionBarActivity;
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ImageView;
    import android.widget.Toast;
    import android.app.AlertDialog;
    import android.content.DialogInterface;
    import android.view.View.OnClickListener;
    import com.natanrosenfeld.texteditor.R;
    import java.util.concurrent.atomic.AtomicInteger;
    import android.os.Handler;
    import java.lang.Runnable;

    public class CreditsActivity extends ActionBarActivity{
    private AtomicInteger mCounter = new AtomicInteger();
    private Handler handler = new Handler();
    private Runnable mRunnable = new Runnable() {

        @Override
        public void run() {
            mCounter = new AtomicInteger();
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Utils.onActivityCreateSetTheme(this);
        setContentView(R.layout.activity_credits);
        ImageView img = (ImageView) findViewById(R.id.imageView);
        addClickToImage(img);

    }


    public void addClickToImage(ImageView imageView) {
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                handler.removeCallbacks(mRunnable);
                handler.postDelayed(mRunnable, 1000);
                if (mCounter.incrementAndGet() == 2) {
                    //Display your dialog fragment
                    new AlertDialog.Builder(getApplicationContext())
                            .setTitle("Easter Egg")
                            .setMessage("Easter Egg...")
                            .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    // continue with delete
                                }
                            })
                            .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    // do nothing
                                }
                            })
                            .show();
                    }
                }
            });
        }
    }



ddmlib: Broken pipe
java.io.IOException: Broken pipe
at sun.nio.ch.FileDispatcherImpl.write0(Native Method)
at sun.nio.ch.SocketDispatcher.write(SocketDispatcher.java:47)
at sun.nio.ch.IOUtil.writeFromNativeBuffer(IOUtil.java:93)
at sun.nio.ch.IOUtil.write(IOUtil.java:65)
at sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:487)
at com.android.ddmlib.JdwpPacket.writeAndConsume(JdwpPacket.java:213)
at com.android.ddmlib.Client.sendAndConsume(Client.java:675)
at com.android.ddmlib.HandleHeap.sendREAQ(HandleHeap.java:342)
at com.android.ddmlib.Client.requestAllocationStatus(Client.java:521)
at com.android.ddmlib.DeviceMonitor.createClient(DeviceMonitor.java:847)
at com.android.ddmlib.DeviceMonitor.openClient(DeviceMonitor.java:815)
at     
 com.android.ddmlib.DeviceMonitor.deviceClientMonitorLoop(DeviceMonitor.java:633)
at com.android.ddmlib.DeviceMonitor.access$100(DeviceMonitor.java:46)
at com.android.ddmlib.DeviceMonitor$3.run(DeviceMonitor.java:592)

1 个答案:

答案 0 :(得分:2)

即使你想从其他线程操纵计数器,你也可以在这里处理3次点击。它添加了单击侦听器并等待1秒钟以将点击计数器设置为0。

public class MyActivity extends Activity{
private AtomicInteger mCounter = new AtomicInteger();
private Handle handler = new Handler();
private Runnable mRunnable = new Runnable(){
    @Override
    public void run(){
        mCounter = new AtomicInteger();
    }
}

public void onCreate(Bundle savedInstance){
    ...
    ImageView myImage = (ImageView) findViewById(R.id.imageView);
    addClickToImage(myImage);
}

public void addClickToImage(ImageView image){
    image.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            handler.removeCallback(mRunnable);
            handler.postDelayed(mRunnable, 1000);
            if(mCounter.incrementAndGet() == 3){
                //Display your dialog fragment
            }
        }
    });
}
}

错误2 您正在通过提供应用程序上下文来打开对话框。这是一行:

new AlertDialog.Builder(getApplicationContext())

唯一可以打开对话框的上下文是当前的活动上下文,而不是应用程序。所以要解决这个错误:

new AlertDialog.Builder(CreditsActivity.this)