Java 清理一个文件项目

Java 清理一个文件项目,java,android,code-cleanup,Java,Android,Code Cleanup,所以我正在做一个项目,在一个文件中使用了大量代码或至少相当数量的代码,我想知道是否有办法将其清理成多个文件。我只处理过一个文件。所以,当涉及到添加更多文件时,我真的不知道如何进行,所以我会请求帮助清理项目。理想的结果是通过其他文件中的小块来清理CoreActivity,使其更易于理解。或者,如果您看到了一种将某些代码清理成更简单、更不详尽的编写方式的方法,那么请进行修复 CoreActivity.java import android.content.Context; import androi

所以我正在做一个项目,在一个文件中使用了大量代码或至少相当数量的代码,我想知道是否有办法将其清理成多个文件。我只处理过一个文件。所以,当涉及到添加更多文件时,我真的不知道如何进行,所以我会请求帮助清理项目。理想的结果是通过其他文件中的小块来清理CoreActivity,使其更易于理解。或者,如果您看到了一种将某些代码清理成更简单、更不详尽的编写方式的方法,那么请进行修复

CoreActivity.java

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebSettings;
 import android.webkit.WebView;
 import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.TextView;

import com.github.pwittchen.gesture.library.Gesture;
import com.github.pwittchen.gesture.library.GestureListener;
import com.github.pwittchen.swipe.library.rx2.Swipe;
import com.github.pwittchen.swipe.library.rx2.SwipeListener;

 public class CoreActivity extends AppCompatActivity {

//Widgets
ConstraintLayout m_Preference_Toolbar;
ConstraintLayout m_Toolbar;
ConstraintLayout m_Preferences;

EditText m_WebView_Search;
WebView m_WebView;

private Swipe WebSwipe;
private Gesture WebGesture;

//Variables
String Url = "https://www.Google.ca";
int WebS =0;

@Override

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_core);


    //Find Views
    m_Toolbar = findViewById(R.id.m_Toolbar);
    m_Preferences = findViewById(R.id.m_Preferences);
    m_WebView_Search = findViewById(R.id.m_WebView_Search);
    m_WebView = findViewById(R.id.m_WebView);
    m_Preference_Toolbar = findViewById(R.id.m_Preferences_Toolbar);

    m_Preference_Toolbar.setVisibility(View.GONE);


    m_Preferences.setOnTouchListener(new View.OnTouchListener() {
        Animation ToolbarGone,ToolbarPrefVisible;
        int x = 0;
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            m_Preferences.setVisibility(View.GONE);
            x =1;

            ToolbarGone = AnimationUtils.loadAnimation(CoreActivity.this, R.anim.m_toolbar_gone);
            m_Toolbar.startAnimation(ToolbarGone);

            if(m_Preferences.getVisibility() == View.GONE || x == 1) {
                ToolbarPrefVisible = AnimationUtils.loadAnimation(CoreActivity.this, R.anim.m_preference_toolbar_appear);
                m_Preference_Toolbar.startAnimation(ToolbarPrefVisible);
            }
          return false;
        }
    });


    //m_WebView
    final WebSettings m_WebViewSetting = m_WebView.getSettings();
    m_WebView.setWebViewClient(new WebViewClient());
    m_WebViewSetting.setJavaScriptEnabled(true);
    m_WebViewSetting.setSupportZoom(true);
    m_WebViewSetting.setLoadWithOverviewMode(true);
    m_WebViewSetting.setUseWideViewPort(true);
    m_WebView.getSettings().setUserAgentString("Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3");
    m_WebView.loadUrl(Url); //Default HomePage




    //m_WebView_Search TextView and Keyboard
    m_WebView_Search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                if (m_WebView_Search.getText().toString().contains(".com")
                        || m_WebView_Search.getText().toString().contains(".ca")
                        || m_WebView_Search.getText().toString().contains(".net")
                        || m_WebView_Search.getText().toString().contains(".org")) {
                    Url = m_WebView_Search.getText().toString();
                    m_WebView.loadUrl("https://www." + Url);
                    m_WebView_Search.clearFocus();
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(m_WebView_Search.getWindowToken(), 0);
                } else {
                    if (m_WebView_Search.getText().toString().contains("")) {
                        Url = m_WebView_Search.getText().toString();
                        m_WebView.loadUrl("https://www.google.ca/search?q=" + Url);
                        m_WebView_Search.clearFocus();
                        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.hideSoftInputFromWindow(m_WebView_Search.getWindowToken(), 0);
                    }
                }
            }
            return false;
        }
    });
    //Swipe Events WebSwipe
    WebSwipe = new Swipe(350, 700);

    WebSwipe.setListener(new SwipeListener() {
        int Web = 0;
        Animation WebShrink, WebEnlarge;
        @Override
        public void onSwipingLeft(final MotionEvent event) {
            if(m_WebView.canGoForward()){
                m_WebView.goForward();
            }
            else{
                m_WebView.reload();
            }
        }

        @Override
        public void onSwipedLeft(final MotionEvent event) {
        }

        @Override
        public void onSwipingRight(final MotionEvent event) {
            if(m_WebView.canGoBack()){
                m_WebView.goBack();
            }
            else{
                m_WebView.reload();
            }
        }

        @Override
        public void onSwipedRight(final MotionEvent event) {
        }

        @Override
        public void onSwipingUp(final MotionEvent event) {
        }

        @Override
        public void onSwipedUp(final MotionEvent event) {
            //WebView Animation (Enlarge)
            if(Web == 0){
                  WebEnlarge = AnimationUtils.loadAnimation(CoreActivity.this, R.anim.m_webview_enlarge);
                m_WebView.startAnimation(WebEnlarge);
                Web = 1;

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        m_Toolbar.setVisibility(View.GONE);
                    }
                }, 350);
            }
        }

            @Override
        public void onSwipingDown(final MotionEvent event) {
        }

        @Override
        public void onSwipedDown(final MotionEvent event) {
            //WebView Animation (Shrink)
            if(Web == 1){
                WebShrink = AnimationUtils.loadAnimation(CoreActivity.this,R.anim.m_webview_shrink);
                m_WebView.startAnimation(WebShrink);
                m_Preference_Toolbar.setVisibility(View.GONE);
                Web = 0;
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        m_Toolbar.setVisibility(View.VISIBLE);
                    }
                }, 350);
            }
        }
    });

    WebGesture = new Gesture();
    WebGesture.addListener(new GestureListener() {
        @Override public void onPress(MotionEvent motionEvent) {

        }

        @Override public void onTap(MotionEvent motionEvent) {


            }

        @Override public void onDrag(MotionEvent motionEvent) {

        }

        @Override public void onMove(MotionEvent motionEvent) {
        }

        @Override public void onRelease(MotionEvent motionEvent) {
        }

        @Override public void onLongPress(MotionEvent motionEvent) {
        }

        @Override public void onMultiTap(MotionEvent motionEvent, int clicks) {
        }
    });
}

    @Override
    public boolean dispatchTouchEvent (MotionEvent event){
        WebSwipe.dispatchTouchEvent(event);
        WebGesture.dispatchTouchEvent(event);
        return super.dispatchTouchEvent(event);
    }
}

拆分代码的最简单方法是考虑单一责任(根据原则)

作为代码中的一个示例,您的触控监听器可以分开。这需要创建一个扩展touch listener类的类(
View.OnTouchListener
),而不是扩展活动

代码中的此块:

m_Preferences.setOnTouchListener(new View.OnTouchListener() {
    ...
});
将成为MyTouchListener.java:

public class MyTouchListener implements View.OnTouchListener {

    Animation ToolbarGone,ToolbarPrefVisible;
    int x = 0;

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        ... // keep exactly your same code here
    }

}
然后您的活动将更改为:

m_Preferences.setOnTouchListener(new MyTouchListener());

你可以学习oo,但你的代码库并不是那么扩展的。当涉及到减少和拆分代码时,一个好的思考方式是回答以下问题:如何在整个类中保持一个抽象级别。我的意思是:假设你正在处理一个PDF文件,里面有文档、段落和文字。这是三个不同的抽象级别,我将尝试使用至少3个方法或类来处理它们。所以,试着用同样的方法来找到如何分割代码做这件事我该如何在主窗体中调用来使用classI不明白你在问什么