gpt4 book ai didi

java - ViewPagerAdapter 选项卡标题翻译 Android 库 neokree/materialtabs

转载 作者:太空宇宙 更新时间:2023-11-04 13:59:33 25 4
gpt4 key购买 nike

在我的应用程序中,我使用 neokree 的materialtabs 库。我希望我的应用程序支持多种语言,因此我希望选项卡标题支持翻译。但我无法让它发挥作用!ViewPagerAdapter.java 有这个方法“Charsequence getPageTitle(intposition)”所以我做了一个开关(位置)。但现在,例如:

case 0:
return R.string.title1;

不起作用,因为 R.string.title1 返回 int。但我不能使用 getString 或 getResources 因为它是一个辅助类。我不能使用上下文,因为它说“类不能扩展多个类”。有什么解决办法吗?

这是我的代码:

ViewPagerAdapter.java:

    package jdev.voicerecorder;

import android.app.Activity;
import android.app.Application;
import android.bluetooth.BluetoothClass;
import android.content.Context;
import android.os.Build;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.util.Log;

import java.util.Locale;

class ViewPagerAdapter extends FragmentStatePagerAdapter{
public Context c;
public ViewPagerAdapter(FragmentManager fm){
super(fm);
}







public Fragment getItem(int num){

Log.d("tag", "num " + num);
switch (num){
case 0:
return new Record();
case 1:
return new ShowList();
}
return null;
}

@Override
public int getCount(){
return 2;
}

@Override
public CharSequence getPageTitle(int position){



switch (position){
case 0:
return R.string.title1;
case 1:
return R.string.title2;
}
return null;
}
}

我的Mainactivity.java(我将其命名为RecordingActivity.java):

    package jdev.voicerecorder;

import android.os.Environment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

import java.io.File;

import it.neokree.materialtabs.MaterialTab;
import it.neokree.materialtabs.MaterialTabHost;
import it.neokree.materialtabs.MaterialTabListener;


public class RecordingActivity extends ActionBarActivity implements MaterialTabListener{

private Toolbar toolbar;

MaterialTabHost tabHost;
ViewPager pager;
ViewPagerAdapter pagerAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recording);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

File folder=new File(Environment.getExternalStorageDirectory()
+File.separator
+"/Recordings");
boolean success=true;
if (!folder.exists()){
success=folder.mkdir();
}
if (success){

}else {

}


tabHost = (MaterialTabHost) this.findViewById(R.id.tabHost);
pager = (ViewPager) this.findViewById(R.id.pager);
pagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
pager.setAdapter(pagerAdapter);



pager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
tabHost.setSelectedNavigationItem(position);
}
});

for (int i = 0; i < pagerAdapter.getCount(); i++) {
tabHost.addTab(
tabHost.newTab()
.setText(pagerAdapter.getPageTitle(i))
.setTabListener(this)
);
}
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_recording, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();



return super.onOptionsItemSelected(item);
}

@Override
public void onTabSelected(MaterialTab materialTab) {
pager.setCurrentItem(materialTab.getPosition());

}

@Override
public void onTabReselected(MaterialTab materialTab) {

}

@Override
public void onTabUnselected(MaterialTab materialTab) {

}
}

和 strings.xml:

   <resources>
<string name="app_name">Recorder</string>

<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>

<!-- TODO: Remove or change this placeholder text -->
<string name="hello_blank_fragment">Hello blank fragment</string>
<string name="recordTab">Record</string>
<string name="recordsTab">All records</string>
<string name="allrecordsTab">ALL RECORDS</string>
<string name="editexttitle">Filename</string>
<string name="savingpath">"Recording saved</string>
<string name="exText">Recording</string>
<string name="buttonSave">Save</string>
<string name="buttonRecord">Record</string>
<string name="Recording">Recording</string>
<string name="deleted">File removed</string>
<string name="title1">Record</string>
<string name="title2">All Files</string>
<string name="bla">Bla</string>
</resources>

错误日志是

    Error:(51, 32) error: incompatible types
required: CharSequence
found: int

最佳答案

使用 getResources().getString(...) 而不是 ID

例如:

return getResources().getString(R.string.title1);

关于java - ViewPagerAdapter 选项卡标题翻译 Android 库 neokree/materialtabs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29446790/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com