java http://li2.me/2016/08/make-a-reusable-ui-in-android-app-development.html

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java http://li2.me/2016/08/make-a-reusable-ui-in-android-app-development.html相关的知识,希望对你有一定的参考价值。

/**
 * Created by weiyi.li on 2/19/16
 * li2.me/2016/08/make-a-reusable-ui-in-android-app-development.html
 */
public abstract class BasicActivity extends AppCompatActivity {
    protected TextView mActionTitleView;
    protected TextView mActionBackBtn;
    protected Button mActionCloseBtn;
    protected Fragment mContentFragment;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_basic);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        setupActionBar();
        setupContentFragment();
        inflateOperationToolbar();
    }
    
    /** Subclass must override this method to create content fragment. */
    protected abstract Fragment createContentFragment();

    /** Subclass should override this method to inflate bottom toolbar layout, if it has a bottom toolbar. */
    protected ViewStub inflateOperationToolbar() {
        ViewStub stub = (ViewStub) findViewById(R.id.operationToolbarViewStub);
        return stub;
    }

    /** Subclass should override this method to set actionbar title. */
    protected String getActionBarTitle() {
        return "";
    }

    /**
     * Callback when ActionBar's Back & Close button clicked.
     * Subclass may override these two methods to do more things.
     */
    protected void onActionBackClicked() {
        finish();
    }

    protected void onActionCloseClicked() {
        // Clear all activities in this App.
        finishAffinity();
    }
    
    /** Setup ActionBar with support toolbar. */
    private void setupActionBar() {
        Toolbar topActionbar = (Toolbar) findViewById(R.id.topActionbar);
        setSupportActionBar(topActionbar);
        mActionTitleView  = (TextView) topActionbar.findViewById(R.id.topActionTitle);
        mActionTitleView.setText(getActionBarTitle());

        mActionBackBtn = (TextView) topActionbar.findViewById(R.id.topActionBack);
        mActionBackBtn.setOnClickListener(new DebouncedOnClickListener() {
            @Override
            public void onDebouncedClick(View v) {
                onActionBackClicked();
            }
        });

        mActionCloseBtn = (Button) topActionbar.findViewById(R.id.topActionClose);
        mActionCloseBtn.setOnClickListener(new DebouncedOnClickListener() {
            @Override
            public void onDebouncedClick(View v) {
                onActionCloseClicked();
            }
        });
    }

    /** Setup fragment. */
    private void setupContentFragment() {
        FragmentManager fm = getSupportFragmentManager();
        Fragment fragment = fm.findFragmentById(R.id.contentFragmentContainer);
        if (fragment == null) {
            fragment = createContentFragment();
            if (fragment != null) {
                fm.beginTransaction().add(R.id.contentFragmentContainer, fragment).commit();
            }
        }
        mContentFragment = fragment;
    }
}

以上是关于java http://li2.me/2016/08/make-a-reusable-ui-in-android-app-development.html的主要内容,如果未能解决你的问题,请参考以下文章

Java 布尔运算

java [Java] Java常用代码#java

Java - 35 Java 实例

Java While 循环

Java 字符串

Java If ... Else