You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
93 lines
2.6 KiB
93 lines
2.6 KiB
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);
|
|
}
|
|
}
|
|
|