如何根据文本框中的文本获取EditText?

时间:2015-03-23 17:37:23

标签: android android-layout android-edittext

我有一个自定义日期选择器(不使用DatePickers,因为它们不适合小屏幕)。月,日和年都是名为NumberPickers的自定义视图。

问题是我需要日期选择器上的按钮拉伸,编辑文本根据编辑文本中的文本进行拉伸。数字选择器都是以编程方式添加的。

数字选择器布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layoutNumberPicker"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@null"
android:orientation="vertical" >

<com.mycompany.ediary.NumberPickerButton
    android:id="@+id/increment"
    android:layout_width="wrap_content"
    android:layout_height="42dp"
    android:background="@null"
    android:scaleType="fitXY"
    android:layout_weight="1"
    android:src="@drawable/timepicker_up_btn" />

<EditText
    android:id="@+id/timepicker_input"
    style="?android:attr/textAppearanceMediumInverse"
    android:layout_width="wrap_content"
    android:layout_height="49dp"
    android:background="@drawable/timepicker_input"
    android:clickable="false"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:gravity="center"
    android:singleLine="true"
    android:layout_weight="1"
    android:scaleType="fitXY"
    android:textSize="16dp" />

<com.mycompany.ediary.NumberPickerButton
    android:id="@+id/decrement"
    android:layout_width="wrap_content"
    android:layout_height="41dp"
    android:background="@null"
    android:scaleType="fitXY"
    android:layout_weight="1"
    android:src="@drawable/timepicker_down_btn" />

但是,EditText的宽度都相同,而不是为文本的大小拉伸。如你所见,这一年被切断了。我不知道如何伸展它。当我将EditText s的宽度设置为设置大小而不是wrap_content时,按钮不会拉伸到相同的宽度(它们是9个补丁)。

View from a note 4

显示月,日和年的代码是:

LinearLayout layoutWidgets = new LinearLayout(con);
    layoutWidgets.setId(GlobalVars.getLatestId());
    /*****Set width of Date******/
    layoutWidgets.setMinimumWidth(185);
    layoutWidgets.setOrientation(LinearLayout.HORIZONTAL);
    /*****Set width of Date******/
    params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

    if (GlobalVars.subjectLeftToRight == 1) {
        params.addRule(RelativeLayout.BELOW, btnDTPicker.getId());
        params.addRule(RelativeLayout.ALIGN_LEFT, lblQuestionText.getId());
        params.setMargins(0, 10, 0, 0);
        //params.setMargins(0, 0, 0, 0);

    } else {
        params.addRule(RelativeLayout.BELOW, btnDTPicker.getId());
        params.addRule(RelativeLayout.ALIGN_RIGHT, lblQuestionText.getId());
        params.setMargins(0, 10, 0, 0);
        //params.setMargins(0, 0, 0, 0);
    }

    layoutWidgets.setWeightSum(1);
    layoutWidgets.setLayoutParams(params);

月份:

layoutMonth = new LinearLayout(con);
    layoutMonth.setId(GlobalVars.getLatestId());
    layoutMonth.setOrientation(LinearLayout.VERTICAL);
    layoutMonth.setMinimumWidth(95);
    pickerMonth = new NumberPicker(con);
    pickerMonth.setId(GlobalVars.getLatestId());
    pickerMonth.setupText(147);// [Blank]
    pickerMonth.setTag(tagMonthPicker);
    pickerMonth.setVisibility(View.GONE);
    pickerMonth.setFormatter(NumberPicker.TWO_DIGIT_FORMATTER);
    pickerMonth.setRange(1, 12, GlobalVars.calNamesShort);

LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

    lparams.weight = .3f;
    pickerMonth.setLayoutParams(lparams);
    layoutMonth.addView(pickerMonth);

这一天:

layoutDay = new LinearLayout(con);
    layoutDay.setId(GlobalVars.getLatestId());
    layoutDay.setTag(tagDayLayout);
    layoutDay.setOrientation(LinearLayout.VERTICAL);
    layoutDay.setMinimumWidth(95);
    layoutDay.setVisibility(View.GONE);

    pickerDay = new NumberPicker(con);
    pickerDay.setId(GlobalVars.getLatestId());
    pickerDay.setupText(147);// [Blank]
    pickerDay.setTag(tagDayPicker);
    pickerDay.setVisibility(View.GONE);
    pickerDay.setFormatter(NumberPicker.TWO_DIGIT_FORMATTER);
        lparams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    lparams.weight = .3f;
    pickerDay.setLayoutParams(lparams);
    layoutDay.addView(pickerDay);

年度:

layoutYear = new LinearLayout(con);
    layoutYear.setId(GlobalVars.getLatestId());
    layoutYear.setTag(tagYearLayout);
    layoutYear.setMinimumWidth(95);
    layoutYear.setOrientation(LinearLayout.VERTICAL);
    layoutYear.setVisibility(View.GONE);

    pickerYear = new NumberPicker(con);
    pickerYear.setId(GlobalVars.getLatestId());
    pickerYear.setupText(147);// [Blank]
    pickerYear.setTag(tagYearPicker);
    pickerYear.setVisibility(View.INVISIBLE);
    pickerYear.setRange(minYear, maxYear);

        params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    /** width of year **/
    lparams.weight = .4f;
    params.setMargins(0, 0, 0, 0);
    pickerYear.setLayoutParams(params);
    layoutYear.addView(pickerYear);

将视图添加到布局:

layoutWidgets.addView(layoutMonth);
layoutWidgets.addView(layoutDay);
layoutWidgets.addView(layoutYear);

修改

public NumberPicker(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs);

    mContext = context;

    setOrientation(VERTICAL);
    LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.number_picker, this, true);
    mHandler = new Handler();
    InputFilter inputFilter = new NumberPickerInputFilter();
    mNumberInputFilter = new NumberRangeKeyListener();
    mIncrementButton = (NumberPickerButton)findViewById(R.id.increment);
    mIncrementButton.setOnClickListener(this);
    mIncrementButton.setOnLongClickListener(this);
    mIncrementButton.setNumberPicker(this);
    mDecrementButton = (NumberPickerButton)findViewById(R.id.decrement);
    mDecrementButton.setOnClickListener(this);
    mDecrementButton.setOnLongClickListener(this);
    mDecrementButton.setNumberPicker(this);

    mText = (EditText)findViewById(R.id.timepicker_input);
    /*** set size of number picker **/
    //mText.setWidth(Math.round(convertFromDp(80)));
    mText.setOnFocusChangeListener(this);
    mText.setFilters(new InputFilter[] { inputFilter });
    mText.setRawInputType(InputType.TYPE_CLASS_NUMBER);


    if (!isEnabled()) {
        setEnabled(false);
    }
}

public float convertFromDp(int input) {
    final float scale = getResources().getDisplayMetrics().density;
    return ((input - 0.5f) / scale);
}

1 个答案:

答案 0 :(得分:0)

我的猜测是父线性布局覆盖了大小。我建议您不要使用wrap_content,而是使用特定的dp并将重力设置为居中(或center_horizo​​ntal),以便您的UI在外观上保持一致。假设您解决了问题,如果让它换行,这意味着按钮会移动!

要修复问题,您可以发布父线性布局的布局参数吗?

编辑:再次审核您的代码

回到原来的问题。查看单个日期选择器的父级,您有三个垂直方向的子级NumberButtonEditTextNumberButton。将父级的宽度设置为wrap_content,将NumberButton的宽度设置为match_parent,将EditText的宽度设置为wrap_content。现在应该使EditText成为宽度的控制因子,NumberButtons扩展到更宽,只要没有对父宽度施加约束(没有足够的可用空间,显式大小集)。例如,如果为父线性布局中的一个集合设置为水平,这将起作用。如果它不在这一点上,则父布局的问题不是这些布局本身。