【问题标题】:android progressbar inside edittextedittext内的android进度条
【发布时间】:2010-12-20 20:37:16
【问题描述】:

我想在activity进行一些处理时在editText的右侧显示进度条(spinner),并在完成后隐藏,有没有简单的方法来做到这一点?

【问题讨论】:

    标签: android progress-bar android-edittext


    【解决方案1】:

    我会使用RelativeLayout,类似这样:

    <RelativeLayout>
        <EditText
            android:id="@+id/the_id"/>
        <ProgressBar
            style="?android:attr/progressBarStyleSmall"
            android:layout_alignTop="@id/the_id"
            android:layout_alignBottom="@id/the_id"
            android:layout_alignRight="@id/the_id"/>
    </RelativeLayout>
    

    【讨论】:

    • 谢谢你,这正是我想要的
    【解决方案2】:

    试试这个...

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
    
            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" >
    
                 <EditText
                    android:id="@+id/editText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="8dp"
                    android:ems="10"
                    android:hint="@string/app_name"
                    android:inputType="textNoSuggestions"
                    android:padding="8dp" >
                 </EditText>
    
                 <ProgressBar
                      style="?android:attr/progressBarStyleInverse"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_alignBottom="@id/editText"
                      android:layout_alignRight="@id/editText"
                      android:layout_alignTop="@id/editText" />
                 </RelativeLayout>
            </LinearLayout>
    

    【讨论】:

      【解决方案3】:

      迟到的答案,但是,也许有人会用这个。

      我创建了一个实际上为我工作的新视图。

      所以,首先,创建布局xml:

      <?xml version="1.0" encoding="utf-8"?>
      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:orientation="vertical">
      
      <com.view.InstantAutoComplete
          android:id="@+id/custom_edit_text_edit_text"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:padding="@dimen/layout_padding"/>
      
      <ProgressBar
          android:id="@+id/custom_edit_text_progress_bar"
          style="@style/SmallProgressBar"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentEnd="true"
          android:layout_alignParentTop="true"
          android:visibility="gone"/>
      
      </RelativeLayout>
      

      现在,一个 java 类:

      public class CustomEditText extends RelativeLayout {
      
          private LayoutInflater mInflater = null;
          private AutocompleteEditText mEditText;
          private ProgressBar mProgressBar;
          private boolean mIsRefreshing = false;
      
      
          private int textColor;
      
          private int hintTextColor;
          private String hint;
      
          private int textSize;
      
          public CustomEditText(Context context, AttributeSet attrs) {
              super(context, attrs);
              initViews(attrs);
          }
      
          public CustomEditText(Context context, AttributeSet attrs, int defStyle) {
              super(context, attrs, defStyle);
              initViews(attrs);
          }
      
          private void initViews(AttributeSet attrs) {
              mInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
              mInflater.inflate(R.layout.custom_layout_edit_text, this, true);
              mEditText = (AutocompleteEditText) findViewById(R.id.custom_edit_text_edit_text);
              mProgressBar = (ProgressBar) findViewById(R.id.custom_edit_text_progress_bar);
      
              mProgressBar.getLayoutParams().height = mEditText.getLayoutParams().height - 30;
              mProgressBar.getLayoutParams().width = mEditText.getLayoutParams().height - 30;
              mProgressBar.requestLayout();
      
      
              TypedArray a = getContext().getTheme().obtainStyledAttributes(attrs, R.styleable.CustomEditText, 0, 0);
              try {
                  textColor = a.getInteger(R.styleable.CustomEditText_textColor, R.color.black);
                  hint = a.getString(R.styleable.CustomEditText_hint);
                  hintTextColor = a.getInteger(R.styleable.CustomEditText_hintTextColor, R.color.black);
                  textSize = a.getDimensionPixelSize(R.styleable.CustomEditText_textSize, R.dimen.text_medium_size);
              } finally {
                  a.recycle();
              }
      
              mEditText.setTextColor(textColor);
              mEditText.setHint(hint);
              mEditText.setHintTextColor(hintTextColor);
              mEditText.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
          }
      
          public void setRefreshing(boolean isVisible) {
              if (isVisible) {
                  if (!mIsRefreshing) {
                      mIsRefreshing = true;
                      mProgressBar.setVisibility(VISIBLE);
                  }
              } else {
                  if (mIsRefreshing) {
                      mIsRefreshing = false;
                      mProgressBar.setVisibility(GONE);
                  }
              }
      
      
          }
      
          public AutocompleteEditText getEditText() {
              return mEditText;
          }
      
          public ProgressBar getProgressBar() {
              return mProgressBar;
          }
      }
      

      定义您的属性,以便您可以在 xml 中使用它。在您的 values 文件夹中,创建一个名为 attrs 的 .xml 文件:

      <resources>
      
          <declare-styleable name="CustomEditText">
              <attr name="hint" format="string"/>
              <attr name="textColor" format="color"/>
              <attr name="textSize" format="dimension"/>
              <attr name="hintTextColor" format="color"/>
          </declare-styleable>
      
      </resources>
      

      最后在你的活动/片段中使用它:

      <com.CustomEditText
                          android:id="@+id/reply_to_request_main_course_edit_text"
                          android:layout_width="0dp"
                          android:layout_height="wrap_content"
                          android:layout_margin="@dimen/layout_padding"
                          android:layout_weight="1"
                          app:textColor="@color/black"
                          app:textSize="@dimen/text_medium_size"/>
      

      您在活动/片段中实例化对象。当您想调用进度条使其可见时,只需键入

      mEditText.setRefreshing(true/false);
      

      【讨论】:

      • 能否添加您使用的 color.xml 和 dimen.xml 值?
      【解决方案4】:

      使用 FrameLayout 以最佳方式实现此目的。 FrameLayout 帮助我们在 EditText 中添加 ProgressBar。检查这个:

      <FrameLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content">
      
              <EditText
                  android:id="@+id/et_mobile"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content" />
      
              <ProgressBar
                  android:id="@+id/pb_login"
                  style="?android:attr/progressBarStyleSmall"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_gravity="end|center_vertical"/>
      </FrameLayout>
      

      结果:

      【讨论】:

        猜你喜欢
        • 2018-01-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-02-16
        • 2011-11-10
        • 2011-02-02
        相关资源
        最近更新 更多