我的长文本Autocompletetextview完全出现在屏幕上

时间:2018-01-15 14:55:11

标签: java android autocompletetextview

我需要的是当用户输入字符时,它应该显示建议,并且该建议必须完全显示在屏幕上。显示文本已完成或我的字符串的长文本在自动完成的第二行中连续显示

package com.example.epson.buscadormep;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final String[] values = new String[]{"Cilindros pequeños de oxigeno gaseoso o de aire, de uso medicinal", "Dispositivos que contienen oxigeno liquido",
                "Cilindo de un gas de la dicivion 2.2 para activar las extremidades artiiciales mecanicas", "Articulos medicinales no radiactivos (incluso aerosoles)"};
        //final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,values);
        final ArrayAdapter<String> adapter =new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,values);
        final AutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.txtMercancias);
        autoCompleteTextView.setThreshold(1);//will start working from second character
        autoCompleteTextView.setAdapter(adapter); //setting the adapter data into the AutoCompleteTextView

        autoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // LinearLayout ll = (LinearLayout) view;
                //This will give you the string value of selected list item
                //TextView listItem = (TextView) ll.getChildAt(0);
                //You can do this or apply own logic to find the selected value case


                if (autoCompleteTextView.getText().toString().equals("Cilindros pequeños de oxigeno gaseoso o de aire, de uso medicinal"))
                {
                    startActivity(new Intent( MainActivity.this, Main2Activity.class));
                }
                if (autoCompleteTextView.getText().toString().equals("Dispositivos que contienen oxigeno liquido"))
                {
                    startActivity(new Intent( MainActivity.this, Main3Activity.class));
                }
                if (autoCompleteTextView.getText().toString().equals("Cilindo de un gas de la dicivion 2.2 para activar las extremidades artiiciales mecanicas"))
                {
                    startActivity(new Intent( MainActivity.this, Main4Activity.class));
                }
                if (autoCompleteTextView.getText().toString().equals("Articulos medicinales no radiactivos (incluso aerosoles)"))
                {
                    startActivity(new Intent( MainActivity.this, Main5Activity.class));
                };
            }
        });



    }
}

这是我的activity_main.xml ...我想我必须解决这个问题吗?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/sample_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Mercancias Peligrosas: "
        android:clickable="true"
        android:onClick="onClick"/>

    <AutoCompleteTextView
        android:id="@+id/txtMercancias"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:hint="Escriba mercancias peligrosa"

        android:singleLine="false"
        android:ellipsize="end"
        android:maxLines="2"
        android:inputType="text"
        android:layout_gravity="center"
        android:paddingLeft="4dp"
        />


</LinearLayout>

1 个答案:

答案 0 :(得分:0)

尝试使用为autoCompleteTextView设置的适配器的自定义布局:

MainActivity.java

final ArrayAdapter adapter = new ArrayAdapter(this,R.layout.custom_autocomplete_item,R.id.autoCompleteItem, values);

custom_autocomplete_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" >

   <TextView
    android:id="@+id/autoCompleteItem"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="5dp"
    android:textSize="14sp"
    />

</LinearLayout>