2014-09-27 98 views
0

在我的應用程序中有一個活動說活動A有一個圖像視圖與一些文本視圖。代碼如下Fontpath沒有通過intent

public class EidCardFinal extends Activity { 

     private ImageView imageView; 
     private TextView receiver, sender, messagebody; 
     private Intent intent; 
     private Bundle bundle; 
     private static final int FONT_SELECT = 1; 

     // public String filepath = "MyFileStorage"; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_eid_card_final); 
      intent = getIntent(); 
      String message1 = intent.getStringExtra("RECEIVER"); 
      String message2 = intent.getStringExtra("SENDER"); 
      String message3 = intent.getStringExtra("MESSAGEBODY"); 
      String check_click = intent.getStringExtra("bttnclick"); 
      imageView = (ImageView) findViewById(R.id.imageView1); 
      receiver = (TextView) findViewById(R.id.textView1); 
      sender = (TextView) findViewById(R.id.textView2); 
      messagebody = (TextView) findViewById(R.id.textView3); 
      receiver.setText(message1); 
      sender.setText(message2); 
      messagebody.setText(message3); 

      // Selected image id 
      if ("BUTTONCLICK".equals(check_click)) { 
       String path = intent.getStringExtra("image"); 
       Uri myUri = Uri.parse(path); 
       imageView.setImageURI(myUri); 

      } else { 
       int position = intent.getExtras().getInt("id"); 
       ImageAdapter imageAdapter = new ImageAdapter(this); 

       // ImageView imageView = (ImageView) findViewById(R.id.imageView1); 
       imageView.setImageResource(imageAdapter.thumbIds[position]); 


    case R.id.change_fonts: 
       Intent fontintent = new Intent(); 
       bundle = getIntent().getExtras(); 
       fontintent.putExtras(bundle); 
       fontintent.setClass(getApplicationContext(), FontSelection.class); 
       this.startActivityForResult(fontintent, FONT_SELECT); 


protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     // TODO Auto-generated method stub 
     super.onActivityResult(requestCode, resultCode, data); 

     if (resultCode == RESULT_OK) { 
      if (requestCode == FONT_SELECT) { 
       Bundle pathadd= data.getExtras(); 
       String fontadd = pathadd.getString("FONTPATH"); 

       //intent = getIntent(); 
       /*String message1 = data.getStringExtra("RECEIVER"); 
       String message2 = data.getStringExtra("SENDER"); 
       String message3 = data.getStringExtra("MESSAGEBODY"); 
       //String check_click = intent.getStringExtra("bttnclick"); 
       imageView = (ImageView) findViewById(R.id.imageView1); 
       receiver = (TextView) findViewById(R.id.textView1); 
       sender = (TextView) findViewById(R.id.textView2); 
       messagebody = (TextView) findViewById(R.id.textView3); 
       receiver.setText(message1); 
       sender.setText(message2); 
       messagebody.setText(message3);*/ 

       //bundle = getIntent().getExtras(); 
       Typeface tyfa = Typeface.createFromAsset(getAssets(), fontadd); 
       receiver.setTypeface(tyfa); 
       sender.setTypeface(tyfa); 
       messagebody.setTypeface(tyfa); 
       s 


} 
    } 
} 

從菜單中列出,用戶被引導到另一個活動說從那裏可以爲活動A碼來選擇自定義字體活動B如下

public class FontSelection extends Activity { 



String[] fontpath = { "fonts/android_7.ttf", "fonts/doridrobot.ttf", 
     "fonts/droidsansmono.ttf", "fonts/droidserif-bold.ttf", 
     "fonts/green-avocado.ttf", "fonts/lokicola.ttf", 
     "fonts/outwrite.ttf", "fonts/painting-the-light.ttf", 
     "fonts/roboto-black.ttf", "fonts/roboto-boldcondensed.ttf", 
     "fonts/roboto-medium.ttf", "fonts/roboto-regular.ttf" }; 

String[] fontname = { "android_7", "doridrobot", "droidsansmono", 
     "droidserif-bold", "green-avocado", "lokicola", "outwrite", 
     "painting-the-light", "roboto-black", "roboto-boldcondensed", 
     "roboto-medium", "roboto-regular" }; 

private Intent fontpathintent = new Intent(); 
private Bundle bundle1; 




@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_font_selection); 

    RadioButton radio1 = (RadioButton) findViewById(R.id.radioButton1); 
    Typeface tf1 = Typeface.createFromAsset(getAssets(), fontpath[0]); 
    radio1.setTypeface(tf1); 
    radio1.setText(fontname[0]); 

    RadioButton radio2 = (RadioButton) findViewById(R.id.radioButton2); 
    Typeface tf2 = Typeface.createFromAsset(getAssets(), fontpath[1]); 
    radio2.setTypeface(tf2); 
    radio2.setText(fontname[1]); 

    RadioButton radio3 = (RadioButton) findViewById(R.id.radioButton3); 
    Typeface tf3 = Typeface.createFromAsset(getAssets(), fontpath[2]); 
    radio3.setTypeface(tf3); 
    radio3.setText(fontname[2]); 

    RadioButton radio4 = (RadioButton) findViewById(R.id.radioButton4); 
    Typeface tf4 = Typeface.createFromAsset(getAssets(), fontpath[3]); 
    radio4.setTypeface(tf4); 
    radio4.setText(fontname[3]); 

    RadioButton radio5 = (RadioButton) findViewById(R.id.radioButton5); 
    Typeface tf5 = Typeface.createFromAsset(getAssets(), fontpath[4]); 
    radio5.setTypeface(tf5); 
    radio5.setText(fontname[4]); 

    RadioButton radio6 = (RadioButton) findViewById(R.id.radioButton6); 
    Typeface tf6 = Typeface.createFromAsset(getAssets(), fontpath[5]); 
    radio6.setTypeface(tf6); 
    radio6.setText(fontname[5]); 

    RadioButton radio7 = (RadioButton) findViewById(R.id.radioButton7); 
    Typeface tf7 = Typeface.createFromAsset(getAssets(), fontpath[6]); 
    radio7.setTypeface(tf7); 
    radio7.setText(fontname[6]); 

    RadioButton radio8 = (RadioButton) findViewById(R.id.radioButton8); 
    Typeface tf8 = Typeface.createFromAsset(getAssets(), fontpath[7]); 
    radio8.setTypeface(tf8); 
    radio8.setText(fontname[7]); 

    RadioButton radio9 = (RadioButton) findViewById(R.id.radioButton9); 
    Typeface tf9 = Typeface.createFromAsset(getAssets(), fontpath[8]); 
    radio9.setTypeface(tf9); 
    radio9.setText(fontname[8]); 

    RadioButton radio10 = (RadioButton) findViewById(R.id.radioButton10); 
    Typeface tf10 = Typeface.createFromAsset(getAssets(), fontpath[9]); 
    radio10.setTypeface(tf10); 
    radio10.setText(fontname[9]); 

    RadioButton radio11 = (RadioButton) findViewById(R.id.radioButton11); 
    Typeface tf11 = Typeface.createFromAsset(getAssets(), fontpath[10]); 
    radio11.setTypeface(tf11); 
    radio11.setText(fontname[10]); 

    RadioButton radio12 = (RadioButton) findViewById(R.id.radioButton12); 
    Typeface tf12 = Typeface.createFromAsset(getAssets(), fontpath[11]); 
    radio12.setTypeface(tf12); 
    radio12.setText(fontname[11]); 

} 

public void onRadioButtonClick(View view) { 

    bundle1 = getIntent().getExtras(); 

    // Is the button now checked? 
    boolean checked = ((RadioButton) view).isChecked(); 
    // Check which radio button was clicked 
    switch (view.getId()) { 

    case R.id.radioButton1: 
     if (checked) 
      fontpathintent.putExtra("FONTPATH", fontpath[0]); 

     fontpathintent.setClass(getApplicationContext(),EidCardFinal.class); 
     fontpathintent.putExtras(bundle1); 
     startActivity(fontpathintent); 
     break; 
    case R.id.radioButton2: 
     if (checked) 
      fontpathintent.putExtra("FONTPATH", fontpath[1]); 
     fontpathintent.putExtras(bundle1); 
     fontpathintent.setClass(getApplicationContext(),EidCardFinal.class); 
     startActivity(fontpathintent); 
     break; 
    case R.id.radioButton3: 
     if (checked) 
      fontpathintent.putExtra("FONTPATH", fontpath[2]); 
     fontpathintent.putExtras(bundle1); 
     fontpathintent.setClass(getApplicationContext(),EidCardFinal.class); 
     startActivity(fontpathintent); 
     break; 
    case R.id.radioButton4: 
     if (checked) 
      fontpathintent.putExtra("FONTPATH", fontpath[3]); 
     fontpathintent.putExtras(bundle1); 
     fontpathintent.setClass(getApplicationContext(),EidCardFinal.class); 
     startActivity(fontpathintent); 
     break; 
    case R.id.radioButton5: 
     if (checked) 
      fontpathintent.putExtra("FONTPATH", fontpath[4]); 
     fontpathintent.putExtras(bundle1); 
     fontpathintent.setClass(getApplicationContext(),EidCardFinal.class); 
     startActivity(fontpathintent); 
     break; 
    case R.id.radioButton6: 
     if (checked) 
      fontpathintent.putExtra("FONTPATH", fontpath[5]); 
     fontpathintent.putExtras(bundle1); 
     fontpathintent.setClass(getApplicationContext(),EidCardFinal.class); 
     startActivity(fontpathintent); 
     break; 
    case R.id.radioButton7: 
     if (checked) 
      fontpathintent.putExtra("FONTPATH", fontpath[6]); 
     fontpathintent.putExtras(bundle1); 
     fontpathintent.setClass(getApplicationContext(),EidCardFinal.class); 
     startActivity(fontpathintent); 
     break; 
    case R.id.radioButton8: 
     if (checked) 
      fontpathintent.putExtra("FONTPATH", fontpath[7]); 
     fontpathintent.putExtras(bundle1); 
     fontpathintent.setClass(getApplicationContext(),EidCardFinal.class); 
     startActivity(fontpathintent); 
     break; 
    case R.id.radioButton9: 
     if (checked) 
      fontpathintent.putExtra("FONTPATH", fontpath[8]); 
     fontpathintent.putExtras(bundle1); 
     fontpathintent.setClass(getApplicationContext(),EidCardFinal.class); 
     startActivity(fontpathintent); 
     break; 
    case R.id.radioButton10: 
     if (checked) 
      fontpathintent.putExtra("FONTPATH", fontpath[9]); 
     fontpathintent.putExtras(bundle1); 
     fontpathintent.setClass(getApplicationContext(),EidCardFinal.class); 
     break; 
    case R.id.radioButton11: 
     if (checked) 
      fontpathintent.putExtra("FONTPATH", fontpath[10]); 
     fontpathintent.putExtras(bundle1); 
     fontpathintent.setClass(getApplicationContext(),EidCardFinal.class); 
     startActivity(fontpathintent); 
     break; 
    case R.id.radioButton12: 
     if (checked) 
      fontpathintent.putExtra("FONTPATH", fontpath[11]); 
     fontpathintent.putExtras(bundle1); 
     fontpathintent.setClass(getApplicationContext(),EidCardFinal.class); 
     startActivity(fontpathintent); 
     break; 
    } 
} 

我的問題是列出的,在選擇字體時,活動A沒有變化,我的意思是活動A中的字體保持不變。

改變了代碼,但仍沒有運氣

case R.id.radioButton1: 
     if (checked) 
      fontpathintent.putExtra("FONTPATH", fontpath[0]); 

     // fontpathintent.setClass(getApplicationContext(),EidCardFinal.class); 
     // fontpathintent.putExtras(bundle1); 
     // startActivity(fontpathintent); 
     setResult(1, fontpathintent); 
     finish(); 
     break; 
    case R.id.radioButton2: 
     if (checked) 
      fontpathintent.putExtra("FONTPATH", fontpath[1]); 
     // fontpathintent.setClass(getApplicationContext(),EidCardFinal.class); 
     // fontpathintent.putExtras(bundle1); 
     // startActivity(fontpathintent); 
     setResult(1, fontpathintent); 
     finish(); 
     break; 

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    // TODO Auto-generated method stub 
    super.onActivityResult(requestCode, resultCode, data); 

    if (resultCode == RESULT_OK) { 
     if (requestCode == 1) { 
      // Bundle pathadd= data.getExtras(); 
      String customfont = data.getStringExtra("FONTPATH"); 
      // String fontadd = pathadd.getString("FONTPATH"); 

      // intent = getIntent(); 
      /* 
      * String message1 = data.getStringExtra("RECEIVER"); String 
      * message2 = data.getStringExtra("SENDER"); String message3 = 
      * data.getStringExtra("MESSAGEBODY"); //String check_click = 
      * intent.getStringExtra("bttnclick"); imageView = (ImageView) 
      * findViewById(R.id.imageView1); receiver = (TextView) 
      * findViewById(R.id.textView1); sender = (TextView) 
      * findViewById(R.id.textView2); messagebody = (TextView) 
      * findViewById(R.id.textView3); receiver.setText(message1); 
      * sender.setText(message2); messagebody.setText(message3); 
      */ 

      // bundle = getIntent().getExtras(); 
      Typeface tyfa = Typeface.createFromAsset(getAssets(), 
        customfont); 
      receiver.setTypeface(tyfa); 
      sender.setTypeface(tyfa); 
      messagebody.setTypeface(tyfa); 

     } 

我認爲有一些問題,同時調用onActivityResult(),因爲我已經嘗試下面的代碼行,但沒有結果也是如此。任何人都可以提出什麼問題?

tyfa = Typeface.createFromAsset(getAssets(), 
         "fonts/outwrite.ttf"); 

回答

0

問題就迎刃而解了。實際上,我在setResult()中使用requestCode而不是resultcode,這是罪魁禍首。

0

使用另一個字段,並具有獨特的ID每個項目,然後使用getextra和putextra方法可能會幫助

+0

請您詳細說明,以便我可以檢查... – Tariq 2014-09-28 15:17:00

0

你不應該再次啓動的第一個活動,而不是你應該設置的結果secont活動並關閉它。第一項活動將以onActivityResult方法獲得結果。

這裏有一個example如何工作的

+0

對我無效:-( – Tariq 2014-09-27 10:25:55