2016-05-20 2 views
0

Я хочу знать, как можно сфокусироваться от Edit_text на Button_click.Как изменить фокус с редактирования текста при нажатии кнопки?

enter image description here

По умолчанию focus на Edit_text_1, когда я нажимаю Save_Button_1 Я хочу, чтобы удалить focus из Edit_text_1 и установить focus на Edit_text_2 и когда я нажимаю Save_button_2 Я хочу, чтобы удалить focus из Edit_text_2 и установить focus на Edit_text_1.

Редактировать

Вот мой код-

MainActivty.java

public class MainActivity extends AppCompatActivity { 

    EditText et1,et2; 
    TextView tv1,tv2; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     et1 = (EditText)findViewById(R.id.et1); 
     et2 = (EditText)findViewById(R.id.et2); 

     tv1 = (TextView) findViewById(R.id.tv1); 
     tv2 = (TextView) findViewById(R.id.tv2); 

     tv1.setText("edit"); 
     tv1.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       et1.clearFocus(); 
       et2.setFocusable(true); 

//    String str = et1.getText().toString(); 
//    Toast msg = Toast.makeText(getBaseContext(),str,Toast.LENGTH_LONG); 
//    msg.show(); 
      } 
     }); 
     tv2.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       et2.clearFocus(); 
       et1.setFocusable(true); 

//    String str = et2.getText().toString(); 
//    Toast msg = Toast.makeText(getBaseContext(),str,Toast.LENGTH_LONG); 
//    msg.show(); 

      } 
     }); 
    } 

} 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 
     <EditText 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/et1" 
      android:layout_weight="0.1" 
      android:focusableInTouchMode="true" 
      android:nextFocusDown="@+id/et2"/> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/tv1" 
      android:text="Save" 
      android:textSize="20sp" 
      android:layout_margin="10dp"/> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 
     <EditText 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/et2" 
      android:layout_weight="0.1" 
      android:nextFocusUp="@id/et1"/> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Save" 
      android:id="@+id/tv2" 
      android:textSize="20sp" 
      android:layout_margin="10dp"/> 
    </LinearLayout> 


</LinearLayout> 

Может ли кто-нибудь помочь мне. Спасибо за аванс.

ответ

2

Попробуйте это,

YOUR_EDITEXT_NAME.requestFocus(); 
+0

thanx это прекрасно работает – Sourabh

0

попробовать этот код:

property in your xml file. For example (for edittext1): 

    android:nextFocusDown="@+id/edittext2" 
+0

Он работает только с edtitext2 до edittext1, но не работает над edittext1 и edittext2, а в xml я пытаюсь использовать android: nextFocusDown = "@ + id/edittext2" для edittext1 и android: nextFocusUp = "@ + id/edittext1" для edittext2 – Sourabh

0

Вы можете получить, используя следующую строку кода в соответствии с вашими требованиями

editText.requestFocus(); 
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT); 

и четкой направленности

editText.clearFocus(); 
Смежные вопросы