Java 编辑文本编辑框

Java 编辑文本编辑框,java,android,android-edittext,Java,Android,Android Edittext,我想给用户选项,使文本样式像中 此图像为edittext格式(粗体斜体和普通选项)。首先用户选择文本,然后单击按钮更改用edittext编写的文本的文本样式。 : 这是密码 public class Addnew extends Activity { Spinner spinner; String select=null; private String selec(String selected){ return selected; } @Override protect

我想给用户选项,使文本样式像中 此图像为edittext格式(粗体斜体和普通选项)。首先用户选择文本,然后单击按钮更改用edittext编写的文本的文本样式。 :

这是密码

public class Addnew extends Activity {
Spinner spinner;
String select=null;
private String selec(String selected){
    return selected;

}

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.addnew);
    spinner = (Spinner) findViewById(R.id.comptypeinput);
    ArrayAdapter<CharSequence> spin= ArrayAdapter.createFromResource(Addnew.this,R.array.spinner,android.R.layout.simple_spinner_item);
    spin.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
    spinner.setAdapter(spin);

    spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        String selected = null;
        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            selected = arg0.getItemAtPosition(arg2).toString();
            select = selec(selected);
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            Toast.makeText(Addnew.this, "Please Select Complaint Type", Toast.LENGTH_LONG).show();
        }
    });


    Button submit = (Button) findViewById(R.id.insert);
    submit.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            EditText comptimee= (EditText) findViewById(R.id.timeinput);
            EditText compaddresse= (EditText) findViewById(R.id.addressinput);
            EditText compdesce = (EditText) findViewById(R.id.descriptioninput);
            String username= getIntent().getStringExtra("user");
            String comptime = comptimee.getText().toString();
            String compaddress = compaddresse.getText().toString();
            String compdesc = compdesce.getText().toString();
            AddnewAsync as=new AddnewAsync();
            as.execute(select,comptime,compdesc,compaddress,username);
        }
    });
}

private class AddnewAsync extends AsyncTask<String, Void, String>{
    private Dialog pd ;
    @Override
    protected String doInBackground(String... params) {
        List<NameValuePair> nvp = new ArrayList<NameValuePair>();
        nvp.add(new BasicNameValuePair("comp_type", params[0]));
        nvp.add(new BasicNameValuePair("availtime", params[1]));
        nvp.add(new BasicNameValuePair("description", params[2]));
        nvp.add(new BasicNameValuePair("address", params[3]));
        nvp.add(new BasicNameValuePair("username", params[4]));
        HttpClient hc = new DefaultHttpClient();
        HttpPost hp = new HttpPost("http://172.31.144.231/test/andcompinsert.php");
        StringBuilder sb = new StringBuilder();
        String result=null;
        try {
            hp.setEntity(new UrlEncodedFormEntity(nvp));
            HttpResponse response = hc.execute(hp);
            HttpEntity ent= response.getEntity();
            InputStream is = ent.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            String line = null;
            while((line=reader.readLine())!= null){
                sb.append(line+"\n");
            }
             result = sb.toString();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
    @Override
    protected void onPreExecute() {
        pd = ProgressDialog.show(Addnew.this, "Please Wait", "Uploading");
        super.onPreExecute();
    }
    @Override
    protected void onPostExecute(String result) {
        pd.dismiss();
        String s = result.trim();
        if(s.equals("success")){
            Toast.makeText(Addnew.this, "Updated Successfully", Toast.LENGTH_LONG).show();
            Intent inten = new Intent(Addnew.this,PreviousComp.class);
            String username= getIntent().getStringExtra("user");
            inten.putExtra("user", username);
            startActivity(inten);
        }else{
            Toast.makeText(Addnew.this, "Error Updating... Please Try Again...", Toast.LENGTH_LONG).show();
        }
        super.onPostExecute(result);
    }
}
}
public类Addnew扩展活动{
纺纱机;
字符串select=null;
私有字符串选择(选定字符串){
返回选中的;
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.addnew);
微调器=(微调器)findViewById(R.id.comptypeinput);
ArrayAdapter spin=ArrayAdapter.createFromResource(Addnew.this,R.array.spinner,android.R.layout.simple\u spinner\u项);
spin.setDropDownViewResource(android.R.layout.simple\u dropdown\u item\u 1line);
旋转器。设置适配器(旋转);
spinner.setOnItemSelectedListener(新的OnItemSelectedListener(){
所选字符串=空;
@凌驾
已选择公共视图(适配器视图arg0、视图arg1、内部arg2、,
长arg3){
selected=arg0.getItemAtPosition(arg2.toString();
选择=选择(已选择);
}
@凌驾
未选择公共无效(AdapterView arg0){
Toast.makeText(Addnew.this,“请选择投诉类型”,Toast.LENGTH_LONG.show();
}
});
按钮提交=(按钮)findViewById(R.id.insert);
submit.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
EditText comptimee=(EditText)findViewById(R.id.timeinput);
EditText compaddresse=(EditText)findViewById(R.id.addressinput);
EditText compdesce=(EditText)findViewById(R.id.descriptioninput);
字符串username=getIntent().getStringExtra(“用户”);
字符串comptime=comptimee.getText().toString();
字符串compaddress=compaddress.getText().toString();
字符串compdesc=compdesce.getText().toString();
AddnewAsync as=newAddNewAsync();
as.execute(选择、comptime、compdesc、compaddress、用户名);
}
});
}
私有类AddnewAsync扩展AsyncTask{
私人对话;
@凌驾
受保护的字符串doInBackground(字符串…参数){
List nvp=new ArrayList();
nvp.add(新的BasicNameValuePair(“复合类型”,参数[0]);
nvp.add(新的BasicNameValuePair(“availtime”,参数[1]);
nvp.add(新的BasicNameValuePair(“说明”,参数[2]);
nvp.add(新的BasicNameValuePair(“地址”,参数[3]);
添加(新的BasicNameValuePair(“用户名”,参数[4]);
HttpClient hc=新的默认HttpClient();
HttpPost hp=新的HttpPost(“http://172.31.144.231/test/andcompinsert.php");
StringBuilder sb=新的StringBuilder();
字符串结果=null;
试一试{
hp.setEntity(新的UrlEncodedFormEntity(nvp));
HttpResponse response=hc.execute(hp);
HttpEntity ent=response.getEntity();
InputStream=ent.getContent();
BufferedReader reader=新的BufferedReader(新的InputStreamReader(is));
字符串行=null;
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
结果=sb.toString();
}捕获(不支持的编码异常e){
e、 printStackTrace();
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
返回结果;
}
@凌驾
受保护的void onPreExecute(){
pd=ProgressDialog.show(Addnew.this,“请稍候”,“正在上载”);
super.onPreExecute();
}
@凌驾
受保护的void onPostExecute(字符串结果){
pd.解散();
字符串s=result.trim();
如果(s.equals(“成功”)){
Toast.makeText(Addnew.this,“更新成功”,Toast.LENGTH_LONG.show();
意向意向=新意向(Addnew.this,PreviousComp.class);
字符串username=getIntent().getStringExtra(“用户”);
inten.putExtra(“用户”,用户名);
星触觉(inten);
}否则{
Toast.makeText(Addnew.this,“更新错误…请重试…”),Toast.LENGTH\u LONG.show();
}
super.onPostExecute(结果);
}
}
}
在我的edittext compdesce中,我需要这些选项。

使用html标记,如
等,对在edittext中输入的文本进行样式设置,并设置文本视图的文本:

final TextView txView=(TextView)findViewById(R.id.textView);
    final EditText txtEdit=(EditText)findViewById(R.id.edit);
    Button bt=(Button)findViewById(R.id.btn);
    bt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String str=txtEdit.getText().toString();
            txView.setText(Html.fromHtml(str));
        }
    });
}
如果在编辑中输入“its a
粗体文本
”这样的文本,文本结果将是:
“这是一个粗体文本”

我们可以看到您试图写入数据库的代码吗?我正在使用asyctask发送http请求并将其保存在数据库中。没有什么特别的。只需一个简单的编辑文本和一个执行asyctask@blahfunkAll的按钮,您就可以完成您的任务。您没有向我们显示任何代码来帮助您。我们不会为您编写代码,但我们可以帮助更正您编写的代码。我们可以看一下您用来写入数据库的代码吗?这就是错误发生的地方。@blahfunk。您必须使用html标记或isert som元素(如stackOverflow)保存文本。例如,堆栈插入**粗体前后,保存完整文本,当需要显示文本时,使用元素检测样式