From 3a79ba8aebcf058ccf72f5068dc3d169cc1b4d34 Mon Sep 17 00:00:00 2001 From: panqihua <1029559041@qq.com> Date: Mon, 23 Mar 2020 03:43:01 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=9B=BD=E9=99=85=E5=8C=96?= =?UTF-8?q?=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle | 5 +- app/src/main/AndroidManifest.xml | 13 ++- .../com/community/pocket/TitleBarView.java | 93 ++++++++++++++++++ .../pocket/data/adapter/LocaleAdapter.java | 8 +- .../com/community/pocket/ui/MainActivity.java | 8 -- .../pocket/ui/login/LoginActivity.java | 97 ++++--------------- .../pocket/ui/register/RegisterActivity.java | 11 +++ app/src/main/res/layout/activity_login.xml | 84 ++++++++-------- app/src/main/res/layout/activity_register.xml | 24 +++++ app/src/main/res/layout/titlebar_view.xml | 10 ++ app/src/main/res/values-en-rUS/dimens.xml | 4 + app/src/main/res/values-en-rUS/strings.xml | 9 +- app/src/main/res/values-zh-rCN/dimens.xml | 4 + app/src/main/res/values-zh-rCN/strings.xml | 9 +- app/src/main/res/values/attrs.xml | 4 + app/src/main/res/values/dimens.xml | 2 + app/src/main/res/values/strings.xml | 13 ++- app/src/main/res/values/styles.xml | 11 ++- 18 files changed, 266 insertions(+), 143 deletions(-) create mode 100644 app/src/main/java/com/community/pocket/TitleBarView.java delete mode 100644 app/src/main/java/com/community/pocket/ui/MainActivity.java create mode 100644 app/src/main/java/com/community/pocket/ui/register/RegisterActivity.java create mode 100644 app/src/main/res/layout/activity_register.xml create mode 100644 app/src/main/res/layout/titlebar_view.xml create mode 100644 app/src/main/res/values-en-rUS/dimens.xml create mode 100644 app/src/main/res/values-zh-rCN/dimens.xml create mode 100644 app/src/main/res/values/attrs.xml diff --git a/app/build.gradle b/app/build.gradle index cdcdd83..5adc89f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -6,7 +6,7 @@ android { defaultConfig { applicationId "com.community.pocket" - minSdkVersion 15 + minSdkVersion 17 targetSdkVersion 29 versionCode 1 versionName "1.0" @@ -31,6 +31,8 @@ dependencies { implementation 'androidx.annotation:annotation:1.1.0' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0' + implementation 'androidx.navigation:navigation-fragment:2.2.1' + implementation 'androidx.navigation:navigation-ui:2.2.1' testImplementation 'junit:junit:4.13' androidTestImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' @@ -38,4 +40,5 @@ dependencies { implementation 'com.github.jokar:multi-languages:0.0.8' implementation 'org.xutils:xutils:3.8.5' implementation 'com.squareup.okhttp3:okhttp:4.4.1' + implementation 'com.wuhenzhizao:titlebar:1.2.0' } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index b3cf93e..fcf330f 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -3,17 +3,16 @@ package="com.community.pocket"> - + android:theme="@style/AppTheme.NoActionBar"> + + + diff --git a/app/src/main/java/com/community/pocket/TitleBarView.java b/app/src/main/java/com/community/pocket/TitleBarView.java new file mode 100644 index 0000000..2eb7e35 --- /dev/null +++ b/app/src/main/java/com/community/pocket/TitleBarView.java @@ -0,0 +1,93 @@ +package com.community.pocket; + +import android.content.Context; +import android.content.Intent; +import android.content.res.TypedArray; +import android.util.AttributeSet; +import android.view.LayoutInflater; +import android.view.View; +import android.widget.AdapterView; + +import androidx.appcompat.widget.AppCompatSpinner; + +import com.community.pocket.data.adapter.LocaleAdapter; +import com.community.pocket.ui.login.LoginActivity; +import com.community.pocket.util.LocalManageUtil; +import com.community.pocket.util.LocaleType; +import com.community.pocket.util.SPUtil; + +/** + * 自定义顶部标题栏 + */ +public class TitleBarView extends AppCompatSpinner { + + public TitleBarView(Context context) { + super(context); + init(null, 0); + } + + public TitleBarView(Context context, AttributeSet attrs) { + super(context, attrs); + init(attrs, 0); + } + + public TitleBarView(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + init(attrs, defStyle); + } + + + private void init(AttributeSet attrs, int defStyle) { + // Load attributes + final TypedArray a = getContext().obtainStyledAttributes( + attrs, R.styleable.TitleBarView, defStyle, 0); + + initSpanner(); + a.recycle(); + } + + private void initSpanner() { + + + //加载语言选项 + this.setAdapter(new LocaleAdapter(LayoutInflater.from(getContext()))); + this.setSelection(SPUtil.getInstance(getContext()).getSelectLanguage()); +// //选择语言事件 + this.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { + @Override + public void onItemSelected(AdapterView parent, View view, int position, long id) { + if (SPUtil.getInstance(getContext()).getSelectLanguage() != position) { + LocaleType locale = LocaleType.getLocale(position); + selectLanguage(locale); + } + } + + @Override + public void onNothingSelected(AdapterView parent) { + + } + }); + + } + + /** + * 选择语言 + * + * @param locale 语言 + */ + private void selectLanguage(LocaleType locale) { + LocalManageUtil.saveSelectLanguage(getContext(), locale.getType()); + reStart(getContext()); + } + + /** + * 重启 + * + * @param context 应用上下文 + */ + public static void reStart(Context context) { + Intent intent = new Intent(context, LoginActivity.class); + intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); + context.startActivity(intent); + } +} diff --git a/app/src/main/java/com/community/pocket/data/adapter/LocaleAdapter.java b/app/src/main/java/com/community/pocket/data/adapter/LocaleAdapter.java index 88e524a..e7fb2a0 100644 --- a/app/src/main/java/com/community/pocket/data/adapter/LocaleAdapter.java +++ b/app/src/main/java/com/community/pocket/data/adapter/LocaleAdapter.java @@ -1,11 +1,14 @@ package com.community.pocket.data.adapter; +import android.os.Build; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; +import androidx.annotation.RequiresApi; + import com.community.pocket.util.LocaleType; import org.xutils.view.annotation.ViewInject; @@ -33,6 +36,7 @@ public class LocaleAdapter extends BaseAdapter { return position; } + @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1) @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder; @@ -45,7 +49,9 @@ public class LocaleAdapter extends BaseAdapter { } else { viewHolder = (ViewHolder) convertView.getTag(); } - viewHolder.childName.setText(convertView.getResources().getText(LocaleType.getLocale(position).getResId())); + String text = convertView.getResources().getString(LocaleType.getLocale(position).getResId()); + viewHolder.childName.setTextAlignment(View.TEXT_ALIGNMENT_CENTER); + viewHolder.childName.setText(text); return convertView; } diff --git a/app/src/main/java/com/community/pocket/ui/MainActivity.java b/app/src/main/java/com/community/pocket/ui/MainActivity.java deleted file mode 100644 index 86f39ad..0000000 --- a/app/src/main/java/com/community/pocket/ui/MainActivity.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.community.pocket.ui; - -import android.annotation.SuppressLint; - -@SuppressLint("Registered") -public class MainActivity extends BaseActivity { - -} diff --git a/app/src/main/java/com/community/pocket/ui/login/LoginActivity.java b/app/src/main/java/com/community/pocket/ui/login/LoginActivity.java index 54c7dfb..0fadf14 100644 --- a/app/src/main/java/com/community/pocket/ui/login/LoginActivity.java +++ b/app/src/main/java/com/community/pocket/ui/login/LoginActivity.java @@ -1,20 +1,15 @@ package com.community.pocket.ui.login; import android.app.Activity; -import android.content.Context; -import android.content.Intent; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.view.KeyEvent; -import android.view.LayoutInflater; import android.view.View; import android.view.inputmethod.EditorInfo; -import android.widget.AdapterView; import android.widget.Button; import android.widget.EditText; import android.widget.ProgressBar; -import android.widget.Spinner; import android.widget.TextView; import android.widget.Toast; @@ -24,14 +19,11 @@ import androidx.lifecycle.Observer; import androidx.lifecycle.ViewModelProvider; import com.community.pocket.R; -import com.community.pocket.data.adapter.LocaleAdapter; import com.community.pocket.ui.BaseActivity; -import com.community.pocket.util.LocalManageUtil; -import com.community.pocket.util.LocaleType; import com.community.pocket.util.PropertiesUtil; -import com.community.pocket.util.SPUtil; import org.xutils.view.annotation.ContentView; +import org.xutils.view.annotation.Event; import org.xutils.view.annotation.ViewInject; /** @@ -43,10 +35,6 @@ public class LoginActivity extends BaseActivity { private LoginViewModel loginViewModel; - //选择语言 - @ViewInject(R.id.selectLang) - private Spinner spinner; - //用户名 @ViewInject(R.id.username) private EditText usernameEditText; @@ -66,7 +54,6 @@ public class LoginActivity extends BaseActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); -// setContentView(R.layout.activity_login); loginViewModel = new ViewModelProvider(this, new LoginViewModelFactory()).get(LoginViewModel.class); @@ -109,25 +96,6 @@ public class LoginActivity extends BaseActivity { }); - //加载语言选项 - spinner.setAdapter(new LocaleAdapter(LayoutInflater.from(this))); - spinner.setSelection(SPUtil.getInstance(getBaseContext()).getSelectLanguage()); - //选择语言事件 - spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { - @Override - public void onItemSelected(AdapterView parent, View view, int position, long id) { - if (SPUtil.getInstance(getBaseContext()).getSelectLanguage() != position) { - LocaleType locale = LocaleType.getLocale(position); - selectLanguage(locale); - } - } - - @Override - public void onNothingSelected(AdapterView parent) { - - } - }); - // 监听用户密码文本输入框内容改变 TextWatcher afterTextChangedListener = new TextWatcher() { @Override @@ -150,32 +118,27 @@ public class LoginActivity extends BaseActivity { //添加监听器到组件 usernameEditText.addTextChangedListener(afterTextChangedListener); passwordEditText.addTextChangedListener(afterTextChangedListener); + } - //监听密码软键盘输入 - passwordEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() { - - @Override - public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { - //点击完成执行登陆操作 - if (actionId == EditorInfo.IME_ACTION_DONE) { - loginViewModel.login(usernameEditText.getText().toString(), - passwordEditText.getText().toString()); - } - //默认不执行任何操作 - return false; - } - }); + //登录按钮触发登录请求操作 + @Event(value = R.id.login) + private void login(View v) { + //显示登录请求处理进度 + loadingProgressBar.setVisibility(View.VISIBLE); + loginViewModel.login(usernameEditText.getText().toString(), + passwordEditText.getText().toString()); + } - //登录按钮触发登录请求操作 - loginButton.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - //显示登录请求处理进度 - loadingProgressBar.setVisibility(View.VISIBLE); - loginViewModel.login(usernameEditText.getText().toString(), - passwordEditText.getText().toString()); - } - }); + //监听密码软键盘输入 + @Event(type = TextView.OnEditorActionListener.class, value = R.id.password) + private boolean onEditorAction(TextView v, int actionId, KeyEvent event) { + //点击完成执行登陆操作 + if (actionId == EditorInfo.IME_ACTION_DONE) { + loginViewModel.login(usernameEditText.getText().toString(), + passwordEditText.getText().toString()); + } + //默认不执行任何操作 + return false; } //登陆成功信息 @@ -190,24 +153,4 @@ public class LoginActivity extends BaseActivity { Toast.makeText(getApplicationContext(), errorString, Toast.LENGTH_SHORT).show(); } - /** - * 选择语言 - * - * @param locale 语言 - */ - private void selectLanguage(LocaleType locale) { - LocalManageUtil.saveSelectLanguage(this, locale.getType()); - reStart(this); - } - - /** - * 重启 - * - * @param context 应用上下文 - */ - public static void reStart(Context context) { - Intent intent = new Intent(context, LoginActivity.class); - intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); - context.startActivity(intent); - } } diff --git a/app/src/main/java/com/community/pocket/ui/register/RegisterActivity.java b/app/src/main/java/com/community/pocket/ui/register/RegisterActivity.java new file mode 100644 index 0000000..b0cde42 --- /dev/null +++ b/app/src/main/java/com/community/pocket/ui/register/RegisterActivity.java @@ -0,0 +1,11 @@ +package com.community.pocket.ui.register; + +import com.community.pocket.R; + +import org.xutils.view.annotation.ContentView; + +@ContentView(R.layout.activity_register) +public class RegisterActivity { + + +} diff --git a/app/src/main/res/layout/activity_login.xml b/app/src/main/res/layout/activity_login.xml index 7ab34d9..cadf781 100644 --- a/app/src/main/res/layout/activity_login.xml +++ b/app/src/main/res/layout/activity_login.xml @@ -11,6 +11,20 @@ android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".ui.login.LoginActivity"> + + + app:layout_constraintTop_toTopOf="parent" /> - -