2014-04-05 5 views
1

입력 할 때 자동 완성 예측이 표시됩니다. 내가 "Joh"에 들어갈 때처럼, 나는 "John F. Kennedy"를 얻는다. 이제 사용자가 "John F. Kennedy"를 클릭하면 John F. Kennedy에 대한 정보를 제공하는 활동으로 이동합니다. 이것을 얻으려면 어떻게해야합니까? 자동 완성 예측을 클릭 할 수있게하려면 어떻게해야합니까?자동 완성 예측을 클릭 할 수있게하려면 어떻게해야합니까?

package com.mavenmaverick.autocomplete_test; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.widget.ArrayAdapter; 
import android.widget.AutoCompleteTextView; 


public class MainActivity extends Activity { 

String[] presidents= { "John F. Kennedy", 
        "Lyndon B. Johnson", 
        "Richard Nixon", 
        "Gerald Ford", 
        "Jimmy Carter", 
        "Ronald Reagan", 
        "George H. W. Bush", 
        "Bill Clinton", 
        "George W. Bush", 
        "Barack Obama" 
        }; 

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

    AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1); 
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.select_dialog_item, presidents); 

    textView.setThreshold(3); 
    textView.setAdapter(adapter); 

} 

} 

Autocomplete_Test

당신은 코드 아래에 시도 할 수

답변

0

,

autoCompleteTextview.setOnItemClickListener(new OnItemClickListener() { 
    @Override 
    public void onItemClick (AdapterView<?> parent, View view, int position, long id) { 
     //... your stuff 
    } 
}) 

그리고 당신은 사용자가 실제로 드롭 다운 목록에서 항목을 선택하면 클릭 리스너를 설정하는 또 하나의 옵션이 있습니다 setOnItemSelectedListener를 사용합니다.

autoCompleteTextview.setOnSelectedListener(new OnItemClickListener() { 
    @Override 
    public void onItemSelected (AdapterView<?> parent, View view, int position, long id) { 
     //... your stuff 
    } 
}); 

편집

if(position == 1){ 
Intent i = new Intent (MainActivity.this, johncanedy.class); 
startActivity(i); 
}else if (position == 2){ 
Intent i = new Intent (MainActivity.this, lyndon.class); 
startActivity(i); 
} 

등등 ...

+0

내 소스 코드를 게시하고 있습니다. 친절하게 대답 해 주시겠습니까? – CodeWalker

+0

@RahulShaw, 내 편집을 확인하고 위의 코드로 위치를 얻은 다음 pos가 1이면 조건을 확인한 다음 pos가 2이면 다시 두 번째 화면으로 이동합니다. – InnocentKiller

+0

@RahulShaw, 내 편집을 확인하십시오. 그것은 당신을 도울 것입니다. – InnocentKiller

0

사용이 코드.

String[] presidents= { "John F. Kennedy", 
        "Lyndon B. Johnson", 
        "Richard Nixon", 
        "Gerald Ford", 
        "Jimmy Carter", 
        "Ronald Reagan", 
        "George H. W. Bush", 
        "Bill Clinton", 
        "George W. Bush", 
        "Barack Obama" 
        }; 
private AutoCompleteTextView myAutoComplete; 

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

     myAutoComplete = (AutoCompleteTextView) findViewById(R.id.myautocomplete); 
     myAutoComplete.addTextChangedListener(this); 
     myAutoComplete.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, presidents)); 



myAutoComplete.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       Intent intent = new Intent(getBaseContext(), InforActivity.class); 
        intent.putExtra("president_name", obj.toString()); 
        startActivity(intent); 
        finish(); 

      } 
     }); 

} 
관련 문제