Fontpath不通过intent传递

时间:2014-09-27 07:38:40

标签: android android-intent

在我的应用程序中有一个活动说活动A,其中包含带有一些文本视图的图像视图。代码列在下面

    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


}
    }
}

从菜单中,用户将被带到另一个活动,例如活动B,其中可以为活动A选择自定义字体。代码列在下面

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");

3 个答案:

答案 0 :(得分:0)

使用另一个字段并为每个项目提供唯一ID然后使用getextra和putextra方法可能会有所帮助

答案 1 :(得分:0)

您不应该再次启动第一个活动,而应该设置secont活动的结果并关闭它。然后,第一个活动将使用onActivityResult方法获得结果。

这是example如何运作

答案 2 :(得分:0)

问题解决了。实际上我在setResult()中使用requestCode而不是resultcode,这就是罪魁祸首。