2017-10-05 102 views
0

好吧,我有一個EditText,A Search Button和A Textview與一個大的可滾動文本。如何在Textview中搜索單詞?

當我進入EditText一個字,並按Search Button字被高亮 .Till這一點,這一切工作正常。
但我想要的是,當我輸入一個單詞並按button,它應該突出顯示該單詞,並將相機移動到該特定單詞(我認爲它在android中稱爲聚焦),我不想滾動每個時間來找到我突出顯示的單詞。我輸入的單詞在文本的底部並突出顯示,我無法每次都找到它時滾動它,這非常刺激。所以請幫助!

所以,我想的是,當我搜索一個單詞應該得到 強調,也應該自動出現在屏幕上,而不 我滾動向下或向上。

還有一件事,我想盡快unhighlight字爲EditText上 變空

這裏是我的代碼

import android.graphics.Color; 
 
import android.support.v7.app.AppCompatActivity; 
 
import android.os.Bundle; 
 
import android.view.View; 
 
import android.widget.Button; 
 
import android.widget.EditText; 
 
import android.widget.TextView; 
 

 
import com.xeoh.android.texthighlighter.TextHighlighter; 
 

 
public class MainActivity extends AppCompatActivity { 
 

 
    EditText search; 
 
    TextView textView; 
 
    Button button; 
 

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

 
     search = (EditText)findViewById(R.id.search); 
 
     textView = (TextView) findViewById(R.id.text); 
 

 
     button = (Button)findViewById(R.id.button); 
 

 

 
     button.setOnClickListener(new View.OnClickListener() { 
 
      @Override 
 
      public void onClick(View v) { 
 
       new TextHighlighter() 
 
         .setBackgroundColor(Color.parseColor("#FFFF00")) 
 
         .setForegroundColor(Color.RED) 
 
         .addTarget(textView) 
 
         .highlight(search.getText().toString(),TextHighlighter.BASE_MATCHER); 
 

 

 
      } 
 
     }); 
 

 

 

 

 

 

 

 

 

 

 

 

 

 
    } 
 
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 
    android:layout_width="match_parent" 
 
    android:orientation="vertical" 
 
    android:layout_height="match_parent"> 
 

 
    <ScrollView 
 
     android:layout_width="match_parent" 
 
     android:layout_height="wrap_content"> 
 

 
     <LinearLayout 
 
      android:layout_width="match_parent" 
 
      android:orientation="vertical" 
 
      android:layout_height="wrap_content"> 
 

 

 
      <EditText 
 
       android:layout_width="match_parent" 
 
       android:layout_height="wrap_content" 
 
       android:id="@+id/search" 
 
       android:hint="Search" 
 
       android:layout_marginTop="20dp"/> 
 

 
      <Button 
 
       android:layout_width="match_parent" 
 
       android:layout_height="wrap_content" 
 
       android:text="Search" 
 
       android:id="@+id/button"/> 
 

 
      <TextView 
 
       android:layout_width="match_parent" 
 
       android:id="@+id/text" 
 
       android:textSize="25sp" 
 
       android:layout_height="wrap_content" 
 
       android:text="@string/test"/> 
 

 

 

 

 
     </LinearLayout> 
 

 

 
    </ScrollView> 
 

 
</LinearLayout>

+0

我認爲如果使用標準TextView來完成這個任務是非常困難的。使用一個獨特的長TextView,你不知道你正在搜索的字符串放在哪裏,你不能檢索這個字符串的位置,以滾動到該座標... – MatPag

+0

可能的重複[編程方式滾動字到視圖中textview](https://stackoverflow.com/questions/21054528/programmatically-scrolling-a-word-into-view-in-a-textview) – NSimon

+0

如果我有多個textviews,每個textview有一個ID,我可以然後通過id搜索整個textview,並將其顯示在屏幕上 –

回答

0

假設你有:

佈局:

<Button 
     android:id="@+id/button" 
     android:text="search!" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <EditText 
     android:id="@+id/editText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <ScrollView 
     android:id="@+id/textViewWrapper" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <TextView 
      android:id="@+id/scrollableText" 
      android:textIsSelectable="true" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 

    </ScrollView> 

然後在你的代碼:

scrollableText = (TextView) findViewById(R.id.scrollableText); 
    editText = (EditText) findViewById(R.id.editText); 
    textViewWrapper = (ScrollView) findViewById(R.id.textViewWrapper); 
    button = (Button) findViewById(R.id.button); 
    scrollableText.setText(R.string.longText); 

    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      String criteria = editText.getText().toString(); 
      String fullText = scrollableText.getText().toString(); 
      if (fullText.contains(criteria)) { 
       int indexOfCriteria = fullText.indexOf(criteria); 
       int lineNumber = scrollableText.getLayout().getLineForOffset(indexOfCriteria); 
       String highlighted = "<font color='red'>"+criteria+"</font>"; 
       fullText = fullText.replace(criteria, highlighted); 
       scrollableText.setText(Html.fromHtml(fullText)); 

       textViewWrapper.scrollTo(0, scrollableText.getLayout().getLineTop(lineNumber)); 
      } 
     } 
    }); 
    editText.addTextChangedListener(new TextWatcher() { 
     @Override 
     public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { 

     } 

     @Override 
     public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { 
      if (charSequence.length() == 0) { 
       String fullText = scrollableText.getText().toString(); 
       fullText = fullText.replace("<font color='red'>", ""); 
       fullText = fullText.replace("</font>", ""); 
       scrollableText.setText(fullText); 
      } 
     } 

     @Override 
     public void afterTextChanged(Editable editable) { 

     } 
    }); 

其中longText是你strings.xml定義了一些長文本。您可能需要在文字上應用其他一些樣式(如我使用<font color='red'>)。

它應該做的工作。我測試了它在Android 8(奧利奧),它工作正常。

+0

謝謝很多人!真的很有用!!!!非常好 –

+0

@FurqanHussain歡迎您。請將答案標記爲正確的答案。 – szamani20

相關問題