gpt4 book ai didi

java - 将字体添加到 Intent 以发送到另一个 Activity

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

我有 2 个 Activity (实际上有 4 个 Activity ,但目前只有 2 个 Activity ),主要 Activity 和字体更改 Activity 。主 Activity 使用 intentstartActivityForResult 将用户输入的文本发送到字体更改 Activity 。在字体更改 Activity 中,用户可以按预期更改文本的字体。我想做的是在用户更改字体后,将文本和新字体发送回主要 Activity 。但我不知道该怎么做。我正在考虑使用某种bundle,但我陷入困境。如果有人可以帮助我,我将不胜感激。

这是我的(相关)主要 Activity 代码:

public class MainActivity extends AppCompatActivity
{
RelativeLayout move_group;

ImageView view;
String backgroundImageName;
SeekBar seekSize;
TextView user_text;
Button button;

SeekBar tiltChange;

String temp;

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

user_text = (TextView) findViewById(R.id.user_text);

static final int REQUEST_FONT = 4;
public void FontChange(View view)
{
Intent fontIntent = new Intent(this, Main4Activity.class);
fontIntent.putExtra("Text", temp);
startActivityForResult(fontIntent, REQUEST_FONT);
}

//There's supposed to be an accompanying onActivityResult method as well
//but i haven't figured that part out yet

}

这是我的(相关)字体 Activity 代码:

public class Main4Activity extends AppCompatActivity
{
TextView user_font_text;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
user_font_text = (TextView) findViewById(R.id.user_font_text);
}

public void boldText(View view)
{
Typeface custom_font = getResources().getFont(R.font.avenir_next_bold);
user_font_text.setTypeface(custom_font);
}

public void italicText(View view)
{
Typeface custom_font = getResources().getFont(R.font.avenir_next_italic);
user_font_text.setTypeface(custom_font);
}

public void regularText(View view)
{
Typeface custom_font = getResources().getFont(R.font.avenir_next_regular);
user_font_text.setTypeface(custom_font);
}

public void thinText(View view)
{
Typeface custom_font = getResources().getFont(R.font.avenir_next_thin);
user_font_text.setTypeface(custom_font);
}

public void OK(View view)
{
//The intent and/or bundle would go here but I don't know what to do
}
}

最佳答案

public void FontChange(View view)
{
temp = user_text.getText().toString();
Intent fontIntent = new Intent(this, Main4Activity.class);
fontIntent.putExtra("Text", temp);
startActivityForResult(fontIntent, REQUEST_FONT);
}

这会将您的字符串从 textview 获取到 fontActivity。

现在在您的 FontActivity 中

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
Intent intent = getIntent();
String text = intent.getStringExtra("temp");
user_font_text = (TextView) findViewById(R.id.user_font_text);
user_font_text.setText(text);
}

public void OK(View view)
{
Intent returnIntent = new Intent();
returnIntent.putExtra("result",
user_font_text.getText().toString());
returnIntent.putExtra("font",
ypur_font_type);
setResult(Activity.RESULT_OK, returnIntent);
}

设置字体样式的剩余部分可以使用 SpannableStringBuilder 完成。在这里您可以查看SpannableStringBuilder 。希望这会有所帮助。

关于java - 将字体添加到 Intent 以发送到另一个 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47606087/

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