EditText,TextView(富文件),Button,RadioButton

来源:互联网 发布:淘宝网店客服兼职 编辑:程序博客网 时间:2024/06/18 14:10

EditText

hint为提示信息,在真正输入信息时会消失。例如:

 android:hint="请输入密码"

password为输入密码时的数字会隐藏。

1)在XML中:
例如:

android:password="true"

2)在代码中:

button.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                password.setTransformationMethod(new PasswordTransformationMethod());            }        });

在按键的点击事件中设置上述代码,这样当按下这个键时密码就不可见了。

设置输入信息的类型

1)inputType

 android:inputType="phone,number等信息"//选择输入的类型,当输入的不是这些类型时就会不能输入。

2)digits

 android:digits="1234567890xX"

将能够输入的字母或字符写入dugits中,这样输入的就只能是这些了。

TextView

1)在文本上加中划线:
在代码中:

  textView.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG);

2)加粗,倾斜

  android:textStyle="italic"//倾斜  android:textStyle="bold"//加粗

TextView中的富文件

在TextView中输入的文件中支持一些HTML形式的文件设置。
例如

我是一个<font color='#ff0000'>文本</font>

这样会使“文本”变成红色。
还可以在文本中加入图片。代码如下:

Spanned spanned = Html.fromHtml("我是一个<font color='#ff0000'>文本</font>然后<img src='ic_lancher'/>中间加一个图标",new Html.ImageGetter(){            @Override            public Drawable getDrawable(String source) {                int id =R.mipmap.ic_launcher;//对id初始化                Class clazz =R.mipmap.class; //获得mipmap类的类                try{                    Field field =clazz.getDeclaredField(source);//获得变量                    id =field.getInt(clazz);//获得变量的值                }catch (NoSuchFieldException e){                    e.printStackTrace();                }catch (IllegalAccessException e){                    e.printStackTrace();                }                Drawable drawable =getResources().getDrawable(id);//获得图片                drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());//设置图片的大小                return drawable;//返回图片            }        },null);        text_fu.setText(spanned);

这里用到了反射。
获得了mipmap类的类,得到了对应字符串的变量,通过变量得到了变量的值,则获得了id值,通过 Drawable drawable =getResources().getDrawable(id);得到图片,通过 drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());设置图片的大小。这样将spanned放入textView中就会显示这些内容了。

Button的背景和状态的变化

1)背景

 android:background="#ffffff"

也可以用图片作为背景

2)按键的变化
当按键未按下时是一种状态,按下时是另一种状态。

在drawble中建立一个文件back:<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:drawable="@mipmap/press" android:state_pressed="true"/>    <item android:drawable="@mipmap/pp"/></selector>//在.xml的Button中: android:background="@drawable/back"

注意这两个的顺序是不能改变的,第一个为按下时的状态。第二个为未按下时的状态。

RadioButton

RadioButton为单选的设置键,一般与RandioGroup一起使用。
在XML中:

 <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/button"        android:text="性别选择"        />    <RadioGroup        android:id="@+id/radio_Group"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:checkedButton="@+id/man"       android:orientation="horizontal"        >        <RadioButton            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="男"            android:id="@+id/man"            />        <RadioButton            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="女"            android:id="@+id/woman"            />    </RadioGroup>

在代码中:

protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        button = (Button) findViewById(R.id.button);        mRadioGroup = (RadioGroup) findViewById(R.id.radio_Group);        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {            @Override            public void onCheckedChanged(RadioGroup group, int checkedId) {                RadioButton rb = (RadioButton) findViewById(checkedId);                String s = (String) rb.getText();//                Toast.makeText(MainActivity.this,"你选择的性别是"+s,Toast.LENGTH_SHORT).show();            }        });        button.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                int checkId = mRadioGroup.getCheckedRadioButtonId();                RadioButton rb = (RadioButton) mRadioGroup.findViewById(checkId);                Toast.makeText(MainActivity.this,"你选择的性别是" + rb.getText(), Toast.LENGTH_SHORT).show();                Log.d("pp",""+rb.getText());                Intent intent =new Intent(MainActivity.this,third.class);                startActivity(intent);            }        });    }
0 0
原创粉丝点击