2016-05-20 50 views
0

我想知道如何从Edit_text关注Button_click如何从点击按钮上的编辑文本更改焦点?

enter image description here

默认focusEdit_text_1当我点击Save_Button_1我想从Edit_text_1删除focusEdit_text_2设置focus,当我点击Save_button_2我想从Edit_text_2删除focusEdit_text_1设置focus

编辑

这里是我的代码 -

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编辑文本1,但它不能在edittext1中编辑文本2,而在xml中为edittext1和android:nextFocusUp =“@ + id/edittext1”尝试使用android:nextFocusDown =“@ + id/edittext2” – Sourabh

0

您可以开始使用这行代码按您的要求

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

和明确的重点

editText.clearFocus(); 
相关问题