RonYamin
RonYamin

Reputation: 77

EditText methods in java android

Hello I'm new to android developing.

Is there a method in java that equals to #.gotFocus?

Is there in java an events list that I can watch and select like in c# visual studio?

I tried to do #.Focus or something similar but had no success.

I want to reproduce the following scheme:

1- EditText has a certain hint => "Enter a value"

2- The user clicks the edit text and the hint disappears => ""

3- The user fills a certain value => "certain value"

Thank's for helpers :)

Upvotes: 0

Views: 2926

Answers (4)

Karol Żygłowicz
Karol Żygłowicz

Reputation: 2452

I'm not familiar with c# but I'm guessing you want event fired when edittext get focus. Try this

 EditText txtEdit= (EditText) findViewById(R.id.edittxt);

 txtEdit.setOnFocusChangeListener(new OnFocusChangeListener() {          

    public void onFocusChange(View v, boolean hasFocus) {
        if(hasFocus){
                    // do the job here when edittext get focus
        }
    }
 });

Upvotes: 0

nunofmendes
nunofmendes

Reputation: 3791

Ron Yamin, If I understand your doubt correctly what you want is:

1- Have a field of text for the user to type words/numbers etc --> It is called EditText in android

2- Have an hint so the user knows what to type --> Eg. "Type your name"

3- And react to focus in some way.

The first one you will achieve either through XML or by code. If you have a main.xml in your layouts folder (assuming you are using eclipse/android studio to develop), you can use the interface to drag an edit text to the android screen.

The second one you will achieve still through the XML. If you right click on it, right side of the screen there will be a little window called Proprieties that you can change things like height and width and a hint. Type there your hint.

Finally the last one you need to go to your code in .java and get a reference of your edit text (findViewById).

Either through setOnClickListener or setOnFocusChangeListener.

More info you can checkout here: http://developer.android.com/guide/topics/ui/controls/text.html

I have googled a tutorial you can check with more detailed information and step by step guide. Hope it helps:

http://examples.javacodegeeks.com/android/core/widget/edittext/android-edittext-example/

Upvotes: 1

bmat
bmat

Reputation: 2153

Assuming your textfield is an instance of EditText (which it probably should be), you can do the following:

textfield.setOnFocusChangeListener(new OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if(hasFocus){
            // this is where you would put your equivalent #.gotFocus logic
        }
    }
});

It's worth noting that the behavior you've described can be achieved by using textfield.setHint. The hint is text that is cleared automatically when the user selects the EditText. It's designed specifically for the case you describe, e.g. textfield.setHint("Enter a Value")

Upvotes: 0

user3629714
user3629714

Reputation:

It seems that you changed your question quite a bit, and my C# ignorance got the best of me.

It seems that what you really want is an EditText, the example text you are looking for is the hint.

You can set the hint in the xml file or by code with .setHint(string) method.

Here's where to start:http://developer.android.com/guide/topics/ui/controls/text.html

edit 3 - events in android are dealt with by using listeners. You can use an onClickListener to achieve what you want.

textView.setOnClickListener(new View.OnClickListener(){
   @Override
   public void onClick(){
     //dostuff
   }
}

Upvotes: 0

Related Questions