Hot questions for Using Android EditText in eclipse

Question:

i'm pretty new with Eclipse and i have to hide the cursor by clearing the focus in the editText after the done button is pressed in the soft keyboard.

I found that this code work for others:

editText.setOnEditorActionListener(new OnEditorActionListener() {        
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
    if(actionId==EditorInfo.IME_ACTION_DONE){
        //Clear focus here from edittext
         editText.clearFocus();
    }
return false;
}
});

the code of the activity

package com.iprogettisti.cppp;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

public class VisualizzaPagina extends ActionBarActivity {

EditText edittext = (EditText) findViewById(R.id.editText1);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (MainActivity.TipoPagina == 1) {
        setContentView(R.layout.fragment_visualizza_pagina);            
    }
    else if (MainActivity.TipoPagina == 2) {
        setContentView(R.layout.fragment_visualizza_pagina2);           
    }
    else if (MainActivity.TipoPagina == 3) {
        setContentView(R.layout.fragment_visualizza_pagina3);           
    }
    else if (MainActivity.TipoPagina == 4) {
        setContentView(R.layout.fragment_visualizza_pagina4);           
    }
    else if (MainActivity.TipoPagina == 5) {
        setContentView(R.layout.fragment_visualizza_pagina5);           
    }

    edittext.setOnEditorActionListener(new OnEditorActionListener() {        
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if(actionId==EditorInfo.IME_ACTION_DONE){
            //Clear focus here from edittext
             edittext.setCursorVisible(false);
        }
    return false;
    }
    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.visualizza_pagina, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}


/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(
                R.layout.fragment_visualizza_pagina, container, false);
        return rootView;
    }
}
}

but i cant figure it out how to get that work. Where should i place that code? And also give me an errore at the first line "editText" as not reconized, i tryied to give that the unique ID of my edittext but it didnt worked.


Answer:

Try this

    EditText edittext = (EditText) findViewById(R.id.edittext1<your unique ID>);


    edittext.setOnEditorActionListener(new OnEditorActionListener() {        
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if(actionId==EditorInfo.IME_ACTION_DONE){
                //Clear focus here from edittext
                 edittext.setCursorVisible(false);
            }
            return false;
        }
    });

    edittext.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) {
                edittext.setCursorVisible(true);
                return false;
            }
    });

Question:

When I was building an app I attempted to place down an editText, but I got this error:

Exception raised during rendering: java.lang.System.arraycopy([CI[CII)V Exception details are logged in Window > Show View > Error Log

After looking this up I came across someone else having this problem (Android app in Eclipse: Edit text not showing on Graphical layout). The problem was that the app was being compiled with KitKat Wear and KitKat wear doesn't support editText.

It should've been simple enough, all I needed to do was click on the green droid on top of the Graphical Layout XML and choose a different API. But for me it was only showing "Automatically Pick Best" and "API 20: Android 4.4W". And Automatically Pick Best just chose 4.4W. So I tried making the app again but when I went to the New Android Application, all that was opening up under Compile With was 4.4W. I tried choosing different Target SDKs and Minimum Required SDKs but nothing worked. Help?


Answer:

You have to download the API 19 or the one that you want. Go to Android SDK Manager and download the API, after that it'll be shown to you in the green droid of the Graphical Layout XML your new downloaded API, choose it and everything is gonna be fine.

Question:

I'm very new to Java and OOP. Here is what I've got:

public class AmountChanged implements View.OnFocusChangeListener {

    @Override
    public void notFocused(View edittext1, boolean focused) {

//Do this awesome stuff

}

How do I instantiate and use this on one of my editText boxes in the mainActivity? I have already declared the editText boxes in the onCreate method.


Answer:

In your class where you are writing the code for the editext,in that activity's onCreate() method, you need to write

yourEditext.setOnFocusChangeListener(new AmountChanged());

Also give an eye to this please as you can use anonymous classes too.

Question:

so I believe I am overthinking this but I want to double check. I am getting my TextView from my EditText widget.

  addMileage.setOnClickListener(new OnClickListener() {

  @Override
  public void OnClick(View v) {
  if (isErase) {
        nextMileage.setText(mileageInput.getText().toString());
  } else {
      nextMileage.setText("");
  }
  isErase = !isErase;
  }

How can I take what the user enters in the EditText then automatically take the number entered and + 3500. Then display in TextView?


Answer:

int result=Integer.parseInt(mileageInput.getText().toString())+3500;
nextMileage.setText(result+"");