Android的添加边框以编程方式编辑文本

Android的添加边框以编程方式编辑文本

本文介绍了Android的添加边框以编程方式编辑文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用this例如,试图将它添加到我的编辑文本编程像 editText.setBackgroundResource(R.drawable.edit_text_back); ,但它不工作。我怎样才能做到这一点?任何建议或想法?

修改的EDITTEXT被编程定义为好。

 的EditText EDITTEXT =新的EditText(this.getApplicationContext());

我加入这一个表行

受审

  editText.setBackground(getResources()getDrawable(R.drawable.edit_text_back));
editText.setBackgroundDrawable(getResources()getDrawable(R.drawable.edit_text_back));

修改文本的创作

 的TableRow行=(的TableRow)findViewById(R.id.table_row_kind);
TableRow.LayoutParams rowP =新TableRow.LayoutParams();
        rowP.setMargins(10,0,0,0);
EDITTEXT =新的EditText(this.getApplicationContext());
EDITTEXT .setGravity(Gravity.FILL_HORIZONTAL);
EDITTEXT .setLayoutParams(rowP);
EDITTEXT .setFilters(新输入过滤器[] {txtFilter});
editText.setBackground(getResources()getDrawable(R.drawable.edit_text_back));

row.xml

 <的TableRow
    机器人:ID =@ + ID / table_row_kind
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:填充=5dip>    <的TextView
       机器人:layout_width =250sp
       机器人:文字=有心人
       机器人:textAppearance =:/>中的Andr​​oid ATTR / textAppearanceLarge?
< /&的TableRow GT;


解决方案

嗯,我也有我解决了通过以下方式相同的问题。它是一个XML文件,把它放在你的绘制文件夹,并设置该XML成的EditText

的背景

活动code:

 的EditText富=(的EditText)findViewById(R.id.editText);
foo.setBackgroundResource(R.drawable.backtext);

backtext.xml

 <形状的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android机器人:形状=矩形>
       [固体机器人:颜色=#FFFFFF/>
       <行程机器人:宽=1dip机器人:颜色=#000000/>
    < /形状>

I used this example and tried to add it to my edit text programmatically like editText.setBackgroundResource(R.drawable.edit_text_back);, but it does not work. How can I accomplish this? Any suggestions or ideas?

EDIT The editText is defined programmatically as well.

EditText editText = new EditText(this.getApplicationContext());

I added this to a table row

TRIED

editText.setBackground(getResources().getDrawable(R.drawable.edit_text_back));
editText.setBackgroundDrawable(getResources().getDrawable(R.drawable.edit_text_back));

EDIT TEXT CREATION

TableRow row = (TableRow) findViewById(R.id.table_row_kind);
TableRow.LayoutParams rowP = new TableRow.LayoutParams();
        rowP.setMargins(10, 0, 0, 0);
editText = new EditText(this.getApplicationContext());
editText .setGravity(Gravity.FILL_HORIZONTAL);
editText .setLayoutParams(rowP);
editText .setFilters(new InputFilter[]{txtFilter});
editText.setBackground(getResources().getDrawable(R.drawable.edit_text_back));

row.xml

<TableRow
    android:id="@+id/table_row_kind"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="5dip" >

    <TextView
       android:layout_width="250sp"
       android:text="Kind"
       android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
解决方案

Well i also have the same issue which i solve by the following way. Its is an xml file put it on your drawable folder and set this xml into the background of that EditText

activity code:

EditText foo = (EditText)findViewById(R.id.editText);
foo.setBackgroundResource(R.drawable.backtext);

backtext.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
       <solid android:color="#ffffff" />
       <stroke android:width="1dip" android:color="#000000"/>
    </shape>

这篇关于Android的添加边框以编程方式编辑文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 13:29