【问题标题】:Android: Autocomplete Text ViewAndroid:自动完成文本视图
【发布时间】:2018-04-24 09:14:49
【问题描述】:

我正在开发一个使用 Google Places 自动完成功能的 android 应用程序。

当我从自动完成列表视图中选择一个项目时,整个字符串会显示在自动完成文本视图中,当它不适合一行时,文本视图会自动变大以适应整个字符串。

我不希望这种情况发生,我希望文本视图保持相同的大小,如果看不到整个字符串也没关系。我该怎么做??

关于自动完成文本视图的另一个问题。我需要在文本视图旁边有一个按钮,以便用户可以提交搜索。建议的列表视图仅与自动完成文本视图一样宽,它不会填满屏幕的整个宽度,因为在文本视图的右侧有一个按钮。我可以让建议列表视图填满屏幕的宽度吗?

【问题讨论】:

    标签: android autocompletetextview


    【解决方案1】:
    1. 我选择android:singleLine,参考singleLine
    2. 如果你想提出建议listview你可以使用这些属性android:dropDownWidth="xx dp"参考这里android:dropDownWidth。您应该在运行时计算屏幕尺寸以获得正确的值。

    其他属性可以帮助你对齐下拉列表视图android:dropDownHorizontalOffsethere

    【讨论】:

    • 非常感谢!!属性 android:dropDownWidth 完美。我将值设置为“match_parent”,下拉列表填充了整个屏幕宽度。
    【解决方案2】:

    试试maxWidth

    android:maxWidth
    
    Makes the TextView be at most this many pixels wide.
    
    Must be a dimension value, which is a floating point number appended with a unit such
    as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp 
    (scaled pixels based on preferred font size), in (inches), mm (millimeters).
    
    This may also be a reference to a resource (in the form "@[package:]type:name") or
    theme attribute (in the form "?[package:][type:]name") containing a value of this type.
    
    This corresponds to the global attribute resource symbol maxWidth.
    
    Related Methods
    
    setMaxWidth(int)
    

    编辑 - 您可以使用 android:imeOptionsactionGo 将“开始”按钮添加到弹出键盘。

    【讨论】:

      【解决方案3】:

      我希望文本视图保持相同的大小,如果 整个字符串都看不到

      您可以使用android:singleLine,参考singleLine

      我可以让建议列表视图填满屏幕的宽度吗?

      不要认为你可以使用 AutoCompleteTextView。您可以参考用户:Bill Gary 的建议,通过添加android:imeOptions="actionGo",您可以在软键盘上拥有Go 按钮。然后,在您的 Activity 上,将 OnEditorActionListener 添加到您的 AutoCompleteTextView :

          actv.setOnEditorActionListener(new OnEditorActionListener() {
      
              public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                  // TODO Auto-generated method stub
                  if (actionId == EditorInfo.IME_ACTION_GO) {
                      Toast.makeText(MainActivity.this, "GO", Toast.LENGTH_SHORT).show();
                      return true;
                  }
                  return false;
              }
          });
      

      【讨论】:

        【解决方案4】:
        AutoCompleteTextView a=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView);
        String s[]={"Monday","Tuesday","Wednesday","Months"};
        ArrayAdapter<String> adapter=new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,s);
        a.setAdapter(adapter);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-05-29
          • 2012-05-16
          • 1970-01-01
          相关资源
          最近更新 更多