【问题标题】:how to change edittext font size如何更改edittext字体大小
【发布时间】:2015-02-05 11:22:37
【问题描述】:

我编写了一个代码来制作一个下拉列表,其中包含一些项目 "8" 、 "12" 、 "13" ...等 像这样的xml:

<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/btn_dropdown"
android:spinnerMode="dropdown" />

现在我在同一布局中有一个带有此标签的编辑文本:

  <EditText
    android:id="@+id/etFName"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10" >
  <requestFocus />
  </EditText>

我想要的是当我选择 8 时,例如 edittext 字体大小将为 8,当我选择 12 时,edittext 字体大小将为 12 等等......

这是我的 Graduation.java 代码

    private Spinner spinner;
private static final String[]paths = {"item 1", "item 2", "item 3"};
 EditText etFName;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.graduation);
    /*spinner = (Spinner)findViewById(R.id.spinner);
    ArrayAdapter<String>adapter = new ArrayAdapter<String>  (Graduation.this,android.R.layout.simple_spinner_item,paths);

    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener((OnItemSelectedListener) this);*/
    Spinner dropdown = (Spinner)findViewById(R.id.spinner);
    String[] items = new String[]{"8", "12", "16", "18", "20", "24", "28"};
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, items);
    dropdown.setAdapter(adapter);
    spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {

            switch (position) {
                case 0:
                    // Whatever you want to happen when the first item gets selected
                    break;
                case 1:
                    // Whatever you want to happen when the second item gets selected
                    break;
                case 2:
                    // Whatever you want to happen when the thrid item gets selected
                    break;

            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
        }
    });

我应该如何填写 cmets 行?

【问题讨论】:

  • EditText#setTextSize...

标签: android android-edittext font-size


【解决方案1】:

我相信你应该试试这个(我没有):

int fontSizeInt;
try
{
    fontSizeInt = Integer.parseInt(items[position]);
}
catch (NumberFormatException e)
{
    fontSizeInt = 12; // Default size.
}
etFName.setTextSize(TypedValue.COMPLEX_UNIT_SP, (float) fontSizeInt);

注意 int 被转换为 float。请记住,您不希望 NumberFormatException 让您措手不及,无论您感觉多么安全。

【讨论】:

    【解决方案2】:
    etFName.setTextSize(TypedValue.COMPLEX_UNIT_SP,Integer.parseInt(items[position]));
    

    【讨论】:

    • 否 - 项目应该以相同的方式工作,所以这里不需要切换
    猜你喜欢
    • 2015-02-05
    • 1970-01-01
    • 2019-02-19
    • 2011-09-24
    • 2015-12-09
    • 2012-10-27
    • 1970-01-01
    • 1970-01-01
    • 2012-03-08
    相关资源
    最近更新 更多