2016-08-02 66 views
-2

我在佈局的工具欄中添加了一個EditText。我想從軟鍵盤按下完成時訪問EditText的值。我已經初始化我的活動的onCreate()中的EditText。我想訪問這個EditText值,並提供給我的API調用。如何訪問在工具欄中添加的EditText的值

這裏ismy XML佈局

<FrameLayout 
    android:id="@+id/toolbar_container" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/app_bar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:theme="@style/Toolbar" > 
     <EditText 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:id="@+id/search_edit_text" 
      android:singleLine="true" 
      android:textCursorDrawable="@drawable/edit_text_cursor" 
      android:backgroundTint="@android:color/white" 
      android:imeOptions="actionSearch" 
      /> 
     </android.support.v7.widget.Toolbar> 

</FrameLayout> 

<android.support.v7.widget.RecyclerView 
    android:id="@+id/rv_ff" 
    android:layout_below="@+id/toolbar_container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 


<ImageView 
    android:id="@+id/iv_ff" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:src="@drawable/stat_no_connection" 
    android:visibility="invisible" /> 

<ProgressBar 
    android:id="@+id/prog_bar_ff" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:layout_centerInParent="true" 
    /> 
<ImageView 
    android:id="@+id/nothing_to_show_here" 
    android:layout_width="match_parent" 
    android:layout_below="@+id/toolbar_container" 
    android:layout_height="match_parent" 
    android:src="@drawable/stat_nothing_gray" 
    android:scaleType="fitCenter" 
    android:layout_centerInParent="true" 
    android:visibility="invisible"/> 

,這是我的onCreate()

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

     final Intent intent = this.getIntent() 
     mUserEmail = intent.getStringExtra(Constants.USER_EMAIL); 
     setContentView(R.layout.search_list_activity); 
     RecyclerView mRecy = (RecyclerView) findViewById(R.id.rv_ff); 
     mProgBar = (ProgressBar) findViewById(R.id.prog_bar_ff); 
     mRecy.setLayoutManager(new LinearLayoutManager(this)); 
     mImageView = (ImageView) findViewById(R.id.iv_ff); 
     nothingToShow = (ImageView) findViewById(R.id.nothing_to_show_here); 

     Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar); 

     /* Common toolbar setup */ 
     setSupportActionBar(toolbar); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     getSupportActionBar().setDisplayShowHomeEnabled(true); 

     searchEditText = (EditText) findViewById(R.id.search_edit_text); 
     Log.d(TAG+"init","search"); 
     mQueryString = searchEditText.getText().toString(); 
     Log.d(TAG+"mQuery",mQueryString); 
     searchEditText.setOnEditorActionListener(new EditText.OnEditorActionListener() { 
      @Override 
      public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 
       if (actionId == EditorInfo.IME_ACTION_DONE) { 
        Log.d(TAG+"mQuery",mQueryString); 
        return true; 
       } 
       return false; 
      } 
     }); 

     mAdapter = new OfferAdapter(this); 
     mRecy.setAdapter(mAdapter); 
    } 

`

+1

searchEditText.getText() –

回答

0

searchEditText在工具欄從工具欄所以得到。

Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar); 
    searchEditText = (EditText) toolbar.findViewById(R.id.search_edit_text); 

    done.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

      String value = searchEditText.getText().toString(); 
      } 
     }); 
+1

searchEditText =(的EditText)findViewById(R.id.search_edit_text);也是正確的。 – sJy

+0

非常感謝你,我得到了結果...非常感謝 –