4

Android Activity 已覆盖方法 onUserInteraction。但是如何检查对话框中的用户交互?我想基于此开发一些逻辑。请帮助此代码与 Activity 一起工作(onUserInteraction)很好 public class MyBaseActivity extends Activity {

public static final long DISCONNECT_TIMEOUT = 300000; // 5 min = 5 * 60 * 1000 ms

private Handler disconnectHandler = new Handler(){
    public void handleMessage(Message msg) {
    }
};

private Runnable disconnectCallback = new Runnable() {
    @Override
    public void run() {
        // Perform any required operation on disconnect
    }
};

public void resetDisconnectTimer(){
    disconnectHandler.removeCallbacks(disconnectCallback);
    disconnectHandler.postDelayed(disconnectCallback, DISCONNECT_TIMEOUT);
}

public void stopDisconnectTimer(){
    disconnectHandler.removeCallbacks(disconnectCallback);
}

@Override
public void onUserInteraction(){
    resetDisconnectTimer();
}

@Override
public void onResume() {
    super.onResume();
    resetDisconnectTimer();
}

@Override
public void onStop() {
    super.onStop();
    stopDisconnectTimer();
}

}

但是如何在 Dialog 中使用 'onUserInteraction' 方法?

button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            final Dialog dialog = new Dialog(MainActivity.this);

            //setting custom layout to dialog
            dialog.setContentView(R.layout.cusotm_dialog_layout);
            dialog.setTitle("Custom Dialog");

            //adding text dynamically
            TextView txt = (TextView) dialog.findViewById(R.id.textView);
            txt.setText("Put your dialog text here.");

            ImageView image = (ImageView)dialog.findViewById(R.id.image);
            image.setImageDrawable(getResources().getDrawable(android.R.drawable.ic_dialog_info));

            //adding button click event
            Button dismissButton = (Button) dialog.findViewById(R.id.button);
            dismissButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
            dialog.show();
        }
4

0 回答 0