Activity with two Layouts and Edittext focusable

Asked

Viewed 80 times

1

My app only has one activity but with two Layouts.

Switching between the two Layouts is done through a Button that exists in each of the Layouts.

In Layout 1 there is a EditText of the kind numerical and in Layout 2 there is another EditText but kind text.

The two Edittexts have the focusable property enabled, but when I switch between Layouts the type of keyboard is not changed automatically, IE, to change the type of keyboard I have to click on Edittext of this Layout.

I’ve tried the java condigo to insert the line edittext2.setFocusable(true) but it doesn’t work.

  • How are you switching between layouts? Post the Activity code.

  • Hello ramaral! I am switching through the Buttons click event. However, thank you for your attention, but I have found the solution: edittext2.requestFocus();

1 answer

1

Easy, very easy:

edittext2.requestFocus();

Here is the complete code:

public class MainActivity extends AppCompatActivity {

    Button botao1;
    Button botao2;
    EditText edittext1;
    EditText edittext2;

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

    public void Layout_1() {
        setContentView(R.layout.layout_1);
        botao1 = (Button) findViewById(R.id.button1);
        edittext1 = (EditText) findViewById(R.id.editText1);
        edittext1.requestFocus();
        botao1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Layout_2();
            }
        });

    }

    public void Layout_2() {
        setContentView(R.layout.layout_2);
        botao2 = (Button) findViewById(R.id.button2);
        edittext2 = (EditText) findViewById(R.id.editText2);
        edittext2.requestFocus();
        botao2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Layout_1();
            }
        });
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.