1

i want to build app with viewpager and display image and text from DB, and i had a button to resize Text to Small,Medium,Big size, but my problem how to make that methode and call it? this is my code

Phrase.java

public void onCreate(Bundle savedInstanceState) {  
     setContentView(R.layout.phrase);  
     mViewPager = (ViewPager) findViewById(R.id.viewPager);  
     mMyFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());  
     mViewPager.setAdapter(mMyFragmentPagerAdapter);

     text = (ImageView)findViewById(R.id.ibText);

     /* call db and store in arraylist */
     ....
}

private static class MyFragmentPagerAdapter extends FragmentPagerAdapter {  
    public MyFragmentPagerAdapter(FragmentManager fm) {  
         super(fm);  
    }  

    @Override  
    public Fragment getItem(int index) {  
         var = arrayphrase.get(index);
         phrase = var.phraseKey;
         title_phrase = var.titleKey;
         sound_id = var.indexkey;
         return PageFragment.newInstance(index,sound_id,phrase,title_phrase);
    }  

    @Override

    public void destroyItem(View container, int position, Object object) {
        // TODO Auto-generated method stub
        super.destroyItem(container, position, object);
    }

    @Override  
    public int getCount() {  

         return NUMBER_OF_PAGES;  
    }  
}  

CharSequence[] sizetext = {"Small", "Medium", "Big","Cancel"};
@Override
public void onClick(View v) {
      case R.id.ibText:
        AlertDialog.Builder listBuilder = new AlertDialog.Builder(Phrase.this);
        listBuilder.setItems(sizetext, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) 
            {

                   if(sizetext[item].equals("Big"))
                   {
                       sizeText=24;
                       onUpdateSize(sizeText);
                   }
                   else if(sizetext[item].equals("Medium"))
                   {
                       sizeText=20;
                       onUpdateSize(sizeText);
                   }
                   else if(sizetext[item].equals("Small"))
                   {
                       sizeText=16;
                       onUpdateSize(sizeText);
                   }
            }
        });
        AlertDialog alertList = listBuilder.create();
        alertList.show();
    break;
    }

PageFragment.java

    public class PageFragment extends Fragment { 
    public static PageFragment newInstance(int index,String sound, String phrase, String title_phrase) {

    PageFragment pageFragment = new PageFragment();
    Bundle bundle = new Bundle();
    bundle.putString("title_phrase", title_phrase);
    bundle.putString("sound", sound);
    bundle.putString("phrase", phrase);
    bundle.putInt("index", index);
    pageFragment.setArguments(bundle);
    return pageFragment;
}

@Override  
public void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
}  

@Override  
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {  

    View view = inflater.inflate(R.layout.fragment, container, false);  
    ImageView imageView = (ImageView) view.findViewById(R.id.imageView1);  
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 2;
    options.inScaled = false;
    options.inPreferQualityOverSpeed=true;
    options.inJustDecodeBounds = false;
    options.inDither = false;
    Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(), Imgid[(int)(Math.random()*Imgid.length)], options);
    //imageView.setScaleType(ScaleType.FIT_XY);
    imageView.setImageBitmap(bitmap);

    ScrollView sv = (ScrollView)view.findViewById(R.id.scrollView1);
    LinearLayout.LayoutParams lp2= new LinearLayout.LayoutParams((int)(TorasanApp.device_width*0.7), (int) (TorasanApp.device_height*0.65),Gravity.CENTER);
    sv.setLayoutParams(lp2);
    sv.setBackgroundColor(Color.argb(20, 65, 65, 65));

    TextView tvdata = (TextView)view.findViewById(R.id.tvDetailPhrase);
    tvdata.setText(getArguments().getString("phrase"));

    TextView tvtitle = (TextView)view.findViewById(R.id.tvTitlephrase);
    tvtitle.setText(getArguments().getString("title_phrase"));
    return view;  
}  
}

you see in my Phrase when onclick i have onUpdateSize(sizeText) and i want call it to change TextView tvdata fontsize that was in PageFragment.java , so how to make that method and call it? because i try but still cant

4

2 回答 2

1

在你FragmentonCreateView,做

mButton.setOnClickListener(new View.OnClickListener() {
    AlertDialog.Builder listBuilder = new AlertDialog.Builder(Phrase.this);
    listBuilder.setItems(sizetext, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) 
        {

               if(sizetext[item].equals("Big"))
               {
                   sizeText=24;
                   onUpdateSize(sizeText);
               }
               else if(sizetext[item].equals("Medium"))
               {
                   sizeText=20;
                   onUpdateSize(sizeText);
               }
               else if(sizetext[item].equals("Small"))
               {
                   sizeText=16;
                   onUpdateSize(sizeText);
               }
        }
    });
    AlertDialog alertList = listBuilder.create();
    alertList.show();
});

然后在您的中创建访问器方法,Activity以便您Fragment可以检索所需的字段。

于 2012-07-25T04:12:21.430 回答
1

在你的问题中,

您可以有一个要更新 textview的Fragment instancein方法,然后您可以从那里调用该类的任何方法(您必须在 Fragment 类中创建这样的方法来更新 textview。)。onClick

如需更多了解,请参阅此链接

在上面的链接第 10 步中包含方法调用(viewer.updateUrl(tutUrl);) 从 FragmentActivity 类片段类

并且在第 8 步中定义了在 Fragment 类中给出的 updateUrl 方法

于 2012-07-25T03:59:09.973 回答