edittext中的粗体和斜体

时间:2018-11-21 14:16:38

标签: java android xml

在编辑文本中,用户应选择键入的内容为粗体,斜体还是普通。在我的情况下,当用户选择粗体时,整个编辑文本将变为粗体,如果用户再次选择斜体,则整个编辑文本将变为斜体。当用户选择某种粗体字时,它应该是粗体字;当他选择斜体字或普通字时,其后键入的单词应该是斜体字或普通字,而不是前一个粗体字。当我在编辑文本中打印输入内容时,即使它以粗体显示,它也能正常打印。

我的代码在底部

<EditText
    android:id="@+id/check_edittext"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="check"
    />

在这里我将编辑文本设置为粗体或斜体

bold.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        editCheck.setTypeface(editCheck.getTypeface(), Typeface.BOLD);
    }
});
italics.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        editCheck.setTypeface(editCheck.getTypeface(), Typeface.ITALIC);
    }
});
normal.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        editCheck.setTypeface(editCheck.getTypeface(), Typeface.NORMAL);
    }
});
button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        result.setText(editCheck.getText().toString());
    }
    });

我该如何实现

2 个答案:

答案 0 :(得分:1)

如果要在文本视图中使用不同的字体样式,可以从HTML进行设置。看一下这篇文章:

Is it possible to have multiple styles inside a TextView?

因此,在您的情况下,您可以保存以HTML格式输入的文本,具体取决于用户单击了以下操作:将<b>设置为粗体,将<i>设置为斜体,等等。

这还将帮助您在结果textView中打印它,因为您将从HTML打印它。

编辑:要解决所有文本都变为粗体或斜体的问题,必须编辑onclick事件以了解所写内容的类型。并且还使用TextWatcher知道插入的文本是什么。例如:

int typeFaceSelected = Typeface.NORMAL //private global var with NORMAL as default value
String resultedText = ""; //private global var, this will give to your textview the result of the edittext in HTML
bold.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        typeFaceSelected = Typeface.BOLD
    }
});
italics.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        typeFaceSelected = Typeface.ITALIC
    }
});
normal.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        typeFaceSelected = Typeface.NORMAL
    }
});

//You can edit the text you add
editCheck.addTextChangedListener(new TextWatcher() {
    public void afterTextChanged(Editable s) {}

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

        public void onTextChanged(CharSequence s, int start, int before, int count) {
            String sAux = s.toString().subString(start, start + count);

            switch(typeFaceSelected)
            {
                case Typeface.NORMAL:
                    resultedText = sAux;
                break;
                case Typeface.ITALIC:
                    resultedText = "<i>" + sAux + "</i>";
                break;
                case Typeface.BOLD:
                    resultedText = "<b>" + sAux + "</b>";
                break;
            }
            editCheck.setText(Html.fromHtml(resultedText));
        }
});

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        result.setText(HTML.fromHTML(resultedText));
    }
    });

以上代码基于此Differences between TextWatcher 's onTextChanged, beforeTextChanged and afterTextChanged

此外,我还没有对其进行测试,所以我不确定它是否有效,但是至少我认为这足以理解这个想法。

答案 1 :(得分:0)

尝试将属性using System.Threading.Tasks; using AutoMapper; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using webapi.DataContext; using webapi.Models; using webapi.ViewModels; namespace webapi.Controllers { [Route("api/controller")] [ApiController] public class AccountsController : ControllerBase { private readonly UserManager<AppUser> _userManager; private readonly IMapper _maper; private readonly ApplicationDbContext _appDbContext; public AccountsController(UserManager<AppUser> userManager, IMapper maper, ApplicationDbContext appDbContext) { _appDbContext = appDbContext; _maper = maper; _userManager = userManager; } [HttpPost] public async Task<IActionResult> Post(RegistrationViewModel model) { if(!ModelState.IsValid){ return BadRequest(ModelState); } var userIdentity = _maper.Map<AppUser>(model); var result = await _userManager.CreateAsync(userIdentity, model.Password); if (!result.Succeeded) return new BadRequestObjectResult(Errors.AddErrorsToModelState(result, ModelState)); await _appDbContext.Customers.AddAsync(new Customer { IdentityId = userIdentity.Id, Location = model.Location }); await _appDbContext.SaveChangesAsync(); return new OkObjectResult("Account created"); } } } 设置为特定xml文件中的EditText