2017-10-05 5 views
0

[OK]を私はEditTextSearch ButtonとA Textview大きなスクロール可能なテキストを持っています。私はEditTextに単語を入力し、Search Buttonを押すとテキストビューで単語を検索するにはどうすればよいですか?

ワードが .Tillこれを強調します、それはすべて正常に動作します。
私が言いたいことは、単語を入力してbuttonを押すと、その単語を強調表示し、その特定の単語にカメラを移動する必要があります(私はそれがアンドロイドで焦点を合わせると思います)、私は毎回スクロールしたくありません私の強調表示された単語を見つける時間。私の入力された単語は、テキストの一番下にあり、強調表示され、私はそれを見つけるために毎回それをスクロールすることはできません、それは非常に刺激的です。

は、だから私は何をしたい、私は単語を検索したとき、それが強調表示 を取得する必要がありますし、また、私はスクロールダウンまたはアップ ことなく、自動的に画面に表示されなければならないことです。 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

[Programmaticallyテキストビュー](https://stackoverflow.com/questions/21054528/programmatically-scrolling-a-word-into-view-in-a-textview) – NSimon

+0

私は複数のtextviewsがあれば、各textviewはidを持っていますIDによってテキストビュー全体を検索し、それを画面に表示してください –

答えて

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> 

longTextはあなたstrings.xmlで定義されたいくつかの長いテキストです

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) { 

     } 
    }); 

。あなたのテキストに他のスタイルを適用したいかもしれません(私が<font color='red'>と同じように)。

それは仕事をする必要があります。私はアンドロイド8(オレオ)でそれをテストし、それは正常に働いた。

+0

ありがとうロットマン!!!それは本当に働いた!!!! Excellent –

+0

@FurqanHussainよろしくお願いします。答えを正解と記入してください。 – szamani20

関連する問題