Android:实现autocompletetextview的意图

时间:2016-10-01 06:47:59

标签: android android-intent autocompletetextview

我是新手,并试图更多地了解意图。我偶然发现autocompletetextview。我如何实现意图,以便不同的建议将进行不同的活动。例如,当我点击第一个建议时,它将移动到第一页,当点击第二个建议时,第二个页面将会出现。

MainActivity.java

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Toast;

public class MainActivity extends Activity {

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

            final String[] List= getResources().getStringArray(R.array.List);
            ArrayAdapter<String> Adapter= new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_dropdown_item_1line,List);

            AutoCompleteTextView AutoComplete=(AutoCompleteTextView)findViewById(R.id.auto);
            AutoComplete.setThreshold(2);
            AutoComplete.setAdapter(Adapter);

            AutoComplete.setOnItemClickListener(new OnItemClickListener() {

                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
                    // TODO Auto-generated method stub

                    Intent intent = new Intent(MainActivity.this, First.class);
                    startActivity(intent);

                    Toast.makeText(getApplicationContext(),(CharSequence)arg0.getItemAtPosition(arg2), Toast.LENGTH_LONG).show();
                    }
                });
            }
     }

的strings.xml

<resources>
    <string name="app_name">AutoComplete</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string-array name="List">
    <item >First</item>
    <item>Second</item>
    </string-array>
</resources>

谢谢。

2 个答案:

答案 0 :(得分:1)

在清单文件中注册您的第一个活动亲爱的

         <activity
            android:name=".First"
            android:label="@string/app_name" >
        </activity>

它将解决您的问题

<强>被修改

Happy Codding !!!

将此代码替换为您的

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
                    // TODO Auto-generated method stub


        if(arg0.getItemAtPosition(arg2).toString().equals("First")){

Intent intent = new Intent(MainActivity.this, First.class);
                    startActivity(intent);
}else if(arg0.getItemAtPosition(arg2).toString().equals("Second")){
Intent intent = new Intent(MainActivity.this, Second.class);
                    startActivity(intent);
}


                    Toast.makeText(getApplicationContext(),(CharSequence)arg0.getItemAtPosition(arg2), Toast.LENGTH_LONG).show();
                    }
                });

答案 1 :(得分:0)

试试这个:

autocompleteview.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected (AdapterView<?> parent, View view, int position, long id) {
        //... your stuff here
    }
    @Override
    public void onNothingSelected (AdapterView<?> parent) {
        //... your stuff here
    }
})