我是一名新手学习者。我正在尝试创建吐司,当用户选择AutoCompleteTextView中的项目时。我正在读取strings.xml中的值,这是正常工作,但我很困惑我如何(甚至处理程序)我需要创建吐司。 下面是我的代码 1- activity.xml
<AutoCompleteTextView android:id="@+id/myautocomplete"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="110dp"
/>
strings.xml
<string-array name="termsarray">
<item>emulator</item>
<item>ant</item>
<item>string.xml</item>
<item>activity</item>
<item>workspace</item>
<item>adt</item>
<item>manifest.xml</item>
<item>package</item>
<item>layout</item>
<item>toast</item>
<item>adb</item>
</string-array>
//activity.java
public class TermActivity extends Activity {
String[] categories;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
categories=getResources().getStringArray(R.array.termsarray);
setContentView(R.layout.activity_term);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_dropdown_item_1line, categories);
AutoCompleteTextView textView =
(AutoCompleteTextView) findViewById(R.id.myautocomplete);
textView.setThreshold(3);
textView.setAdapter(adapter);
}
答案 0 :(得分:8)
在听众上处理。试试这个:
textView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(context, "Position clicked: " + position, Toast.LENGTH_SHORT).show();
}
});
答案 1 :(得分:2)
textView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
String str = (String) parent.getItemAtPosition(position);
Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();
}
});
答案 2 :(得分:1)
textView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
if(textView.getText().toString().equals("string.xml"))
{
Toast.makeText(TermActivity.this, "A string resource provides text strings for your application with optional text styling and formatting", Toast.LENGTH_LONG).show();
//other logic
}
}
});
答案 3 :(得分:0)
textView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
String str = (String) parent.getItemAtPosition(position);
Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();
}
});