简体   繁体   中英

How to modify Android app UI via java when I created it using main.xml file?

This is my first post here. Have done hundreds of posts, mostly answers, in linuxquestions.org, but nothing here yet. I'm starting to develop using android SDK 1.2, and struggle to understand something.

I've used a basic source that uses xml to define a UI and gets it displayed using setContentView(R.layout.main);.

Now in my Java program I'd like to add some code to modify some of the objects I defined in my xml file.

I understand Java, the object concept, methods, etc ... but I don't know which exact syntax to use, and which id my object actually is.

My xml code includes:

<EditText android:layout_width="match_parent" android:id="@+id/editText1" android:layout_height="wrap_content" android:text="@string/saisie">
    <requestFocus></requestFocus>
</EditText>

my .java includes:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);
}

My question is: how do I modify the .java code to make the EditText object display "hello" instead of what is statically defined in my strings file?

I know there is not a big practical use in the example code I am doing, but it will help me do lots of other more interesting things.

In your onCreate() or another function, add:

 EditText et = (EditText)findViewById(R.id.editText1);
 et.setText("some text");

Change to this (delete the android:text=@string/..)

<EditText 
 android:layout_width="match_parent" 
 android:id="@+id/editText1" 
 android:layout_height="wrap_content" 
 <requestFocus></requestFocus>
</EditText>

And add the following in onCreate()

 EditText text = (EditText)findViewById(R.id.editText1);
 text.setText("some text");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM