小编Anu*_*oob的帖子

如何通过重写方法在java枚举中使用字段?

任务是用java实现漂亮的策略设计模式enum:

public enum MyEnum {

    FIRST {
        @Override
        public String doIt() {
            return "1: " + someField; //error
        }
    },
    SECOND {
        @Override
        public String doIt() {
            return "2: " + someField; //error
        }
    };

    private String someField;

    public abstract String doIt();

} 
Run Code Online (Sandbox Code Playgroud)

但是在提到someField我的时候

无法对someField的非静态字段进行静态引用.

有什么不对,有可能做得更好吗?

java enums strategy-pattern

29
推荐指数
2
解决办法
5212
查看次数

我可以将artifactId转换为myven原型中的classname前缀吗?

我正在创建一个maven原型,并且在项目中生成了一个想要一个以生成项目的工件ID命名的类.

工件ID的格式如下:the-project-name并且应该命名类TheProjectNameMain.

我试图在我这样做,archetype-metadata.xml但我无法做到这一点.

<archetype-descriptor>
    <requiredProperties>
        <requiredProperty key="classNamePrefix">
            <defaultValue>${WordUtils.capitalize(artifactId.replaceAll("-", " ")).replaceAll(" ", "")}</defaultValue>
        </requiredProperty>        
    </requiredProperties>
</archetype-descriptor>
Run Code Online (Sandbox Code Playgroud)

正如你所看到我试图使用WordUtils(来自apache-commons),但我猜这是不可用的,因为我收到了一个错误.Error merging velocity templates:.....我也尝试了不同的组合,.replaceAll但我无法获得正确的格式.

在这种情况下,有没有人知道从a-hypenated-string到CamelCaseClassName的方法?

java velocity maven maven-archetype

14
推荐指数
3
解决办法
6823
查看次数

Android移动视图触摸事件

我想在我的布局中移动两个不同的视图,以便用户可以像他的愿望一样显示它.

到目前为止,我已经制作了以下代码来处理触摸事件:

this.viewEvent.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent event)
    {           
        final int y = (int) event.getRawY();

        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) view.getLayoutParams();
        switch (event.getAction() & MotionEvent.ACTION_MASK)
        {
            case MotionEvent.ACTION_DOWN:
                element.setEventY(y - params.topMargin);
                break;

            case MotionEvent.ACTION_UP:
                viewGroup.invalidate();
                break;

            case MotionEvent.ACTION_POINTER_DOWN:
            case MotionEvent.ACTION_POINTER_UP:
                break;

            case MotionEvent.ACTION_MOVE:
                params.topMargin = y - element.getEventY();
                params.bottomMargin = screenHeight - view.getHeight() - params.topMargin;

                // Avoid out of screen
                if (params.topMargin < 0) return true;

                // Apply changes
                view.setLayoutParams(params);
                break;
        }

        return true;
    }
});
Run Code Online (Sandbox Code Playgroud)

element …

android touch ontouchlistener

12
推荐指数
1
解决办法
4万
查看次数

列出.zip目录而不解压缩

我正在用Java构建一个文件浏览器,我在JTrees中列出了文件/文件夹.我现在要做的是当我到达一个压缩文件夹时,我想列出其内容,但不首先提取它.

如果有人有想法,请分享.

java zip

10
推荐指数
2
解决办法
2万
查看次数

检查字符串是否等于引号

我试图检查字符串是否等于引号(").但是,string.equals(""")因为它认为我有一个额外的引号,所以不起作用.如何检查字符串是否等于引号?

java

6
推荐指数
1
解决办法
4649
查看次数

为什么带数组作为参数的构造函数在参数[java]中带有Object的构造函数之前?

我有这个令人困惑的代码:

public class Confusing {
   private Confusing(Object o){
       System.out.println("Object");
   } 
   private Confusing(double[]dArray){
       System.out.println("double array");
   }
   public static void main(String[] args){
       new Confusing(null);
   }
}
Run Code Online (Sandbox Code Playgroud)

当"编译"并运行程序显示"双数组"为什么数组先于Object?是否有其他构造函数情况会发生这种令人困惑的行为?

java constructor

5
推荐指数
1
解决办法
74
查看次数

假设焦点在Selenium chrome浏览器中

我正在使用Selenium从网站上抓取数据.该网站需要窗口焦点,以显示我需要的某些元素.

我希望能够在后台运行我的程序,而不必在窗口运行时对其进行聚焦.

有没有办法欺骗网站认为它专注于?

我正在使用硒铬驱动器.


编辑:这是我建立的快速而肮脏的测试.

查看GitHub上的代码

window.onblur收到活动后,网站背景颜色将变为黑色,并在window.onfocus收到活动时返回白色.

我想假装那些事件,让浏览器认为它已经收到了焦点事件.

python selenium python-2.7 selenium-chromedriver selenium-webdriver

5
推荐指数
1
解决办法
1190
查看次数

稍后在文件中安装Android布局参考xml元素

如何引用以后的XML元素?

这是一个特定的用例.假设我有一个带有LinearLayout根的表单,其中包含多行的LinearLayouts,每行包含一个或多个文本输入区域.

这是我想要的视觉效果.第一张照片来自Venmo的应用程序,第二张是以下XML的渲染.

Venmo的例子 布局

这样的布局可能如下所示:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/row_card_number"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/card_number"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:nextFocusDown="@id/month"/>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/row_date"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/month"
            android:layout_height="wrap_content"
            android:layout_width="100dp"
            android:nextFocusDown="@id/year"/>

        <EditText
            android:id="@+id/year"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"/>
    </LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

在这个用例中,前向引用是必要的,以便设置下一个焦点元素.这样,当您按下键盘上的下一个按钮时,它将转到正确的视图.在这个样本xml中,没有nextFocusDowns,按下next将从名称到月份,并且永远不会去年.

但是,如果您尝试编译它,您将收到一个错误:

错误:(18,36)找不到与给定名称匹配的资源(在'nextFocusDown',值为'@id/month').

这是因为month当我尝试引用它时,id 尚未初始化,因为它稍后在文件中.如何引用文件中稍后出现的xml中的id?

xml android android-layout android-linearlayout

5
推荐指数
1
解决办法
1156
查看次数

OkHTTP Websocket:连接上的蒸汽意外结束

我正在尝试连接到 Stack Exchange 聊天 Websocket。websocket 用于接收聊天中的新事件,例如新消息。

这是用于创建 Websocket 的代码:

String wsUrl = getWsUrl();
Request wsRequest = new Request.Builder()
        .url(wsUrl)
        .build();
WebSocketCall wsCall = WebSocketCall.create(httpClient, wsRequest);
wsCall.enqueue(new ChatWebSocketListener());
Run Code Online (Sandbox Code Playgroud)

websocket URL 格式如下:

wss://chat.sockets.stackexchange.com/events/16/4b3a8a1f68704b8db35ce9f0915c7c45
Run Code Online (Sandbox Code Playgroud)

WebSocketListeneronFailure仅接收响应,但有以下例外:

E/IOException? unexpected end of stream on Connection{chat.sockets.stackexchange.com:443, proxy=DIRECT@ hostAddress=192.111.0.32 cipherSuite=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 protocol=http/1.1} (recycle count=0)
    java.io.IOException: unexpected end of stream on Connection{chat.sockets.stackexchange.com:443, proxy=DIRECT@ hostAddress=192.111.0.32 cipherSuite=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 protocol=http/1.1} (recycle count=0)
            at com.squareup.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:211)
            at com.squareup.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:80)
            at com.squareup.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:917)
            at com.squareup.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:757)
            at com.squareup.okhttp.Call.getResponse(Call.java:274)
            at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:230)
            at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:201)
            at com.squareup.okhttp.Call.access$100(Call.java:36)
            at com.squareup.okhttp.Call$AsyncCall.execute(Call.java:164)
            at com.squareup.okhttp.internal.NamedRunnable.run(NamedRunnable.java:33) …
Run Code Online (Sandbox Code Playgroud)

android websocket okhttp

5
推荐指数
1
解决办法
1万
查看次数

如何让按钮高度填充网站的垂直空间?

我有一个移动网站,它有 3 个按钮和几行文本。我将文本居中(水平),按钮占据屏幕的整个宽度。但是,我需要缩放按钮的高度以占据整个屏幕。如何使按钮垂直缩放?

<p>PROCRASTINATION TIMER v2.0.0</p>

<span id="current">Currently WAITING</span><br/>
<span>Time to Work: </span><span id="work">Not set.</span><br/>
<span>Time to Derp: </span><span id="derp">Not set.</span><br/>

<button onclick="get_workderp_times();" id="startbutton" style="width:100%;height:33%;">Start</button><br/>
<button onclick="switchstatus('Derp');" id="derpbutton" style="width:100%;height:33%;">Derp</button><br/>
<button onclick="switchstatus('Work');" id="workbutton" style="width:100%;height:33%;">Work</button><br/>

<p>Created by Snake Squared Industries</p>
Run Code Online (Sandbox Code Playgroud)

这是镀铬的,高度设置为 33%:

style="width:100%;height:33%;"
Run Code Online (Sandbox Code Playgroud)

镀铬高度:33%

这是镀铬的,没有设置高度:

style="width:100%;"
Run Code Online (Sandbox Code Playgroud)

镀铬无高度

这是镀铬的,高度设置为 100%:

style="width:100%;height:100%;"
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

(顺便说一下,我查了一下,在IE下结果也是一样的...)

html css

4
推荐指数
1
解决办法
3万
查看次数