Android: Install Editing Text or Text View

I am developing an application in which I create an Edittext programmatically as:

EditText edText = new EditText(this); edText.setId(1); edText .setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,0f)); edText .setInputType(InputType.TYPE_CLASS_NUMBER); edText.setHint("dsgsdgsgs"); tableLayout.addView(edText); 

Here I set the Edit text identifier to "1" on the line edText.setId(1); in integer.

But I need - I want to set the identifier as a character, for example:

 edText.setId("edittext_hello"); 

So that I can access it through this identifier. How can I achieve this, please help.

+6
source share
5 answers

You cannot set id as a string. You can assign an integer as Id. But if you want to use String as id for ease of use, then in res / values ​​/ids.xml

 <?xml version="1.0" encoding="utf-8"?> <resources> <item name="edit_text_hello" type="id"/> </resources> 

And then use it like:

 edText.setId(R.id.edit_text_hello); 

So you can do what you need.

+14
source

As others have said, you cannot do this. Why do you want / what do you want?

You can create an identifier in an XML file and use it if you want it to be descriptive. This is also a better approach than using an int literal, as you may encounter an identifier with other views in the layout hierarchy (unlikely, but possible). This seems to me the best / cleanest solution to your problem.

See http://developer.android.com/guide/topics/resources/more-resources.html#Id

eg. in res/values/id.xml

 <?xml version="1.0" encoding="utf-8"?> <resources> <item type="id" name="edittext_hello" /> </resources> 

and then install with

 edText.setId(R.id.edittext_hello); 
+20
source

No, you cannot set id as a string. You can designate integer as Id. You can use setTag() for View with String. But for id, it will be only Integer. Since android resources are supported in R.java file for integer type.

Update:

Why don't you point String or other data types (without integer) id to any android resource?

Because:

The Android Resource Identifier is a 32-bit integer. It contains

 an 8-bit Package id [bits 24-31] an 8-bit Type id [bits 16-23] a 16-bit Entry index [bits 0-15] 

A packet identifier identifies a packet containing a resource.

The type identifier identifies the type of resource and therefore the corresponding piece of Specpec and the Type chunk or chunks that contain its value or value

The Entry Index identifies a single resource in a Typepec chunk and type type (s).

+4
source

You cannot set id with char , String or anything else except int ... because, id supported by an R.java file that contains only int .

You can use setTag() instead of setId() .

Use setTag() as shown below.

 edText.setTag("edittext_hello"); 
+3
source

Instead of setId () you can use setText ().

edText_view.setText ("Your text");

-1
source

All Articles