六、edittext
要使用editext的功能 需要在activity中先聲明edittext舌剂。private Edittext mEdittext;
然后還需要找到這個控件气破。
常用屬性有android:textSize="16sp"
????????android:textColor="#ff0066"
android:background="@drawable/bg_btn3"
android:drawableLeft="@drawable/user"是指圖片在編輯框的位置
android:drawablePadding="5dp"是指距離圖片的空隙多大
android:maxLines="1"最大行數(shù)
android:hint="用戶名" 默認文字 聊浅,輸入內(nèi)容時自動消失
android:inputType="textPassword"輸入內(nèi)容的文字類型
添加編輯框的監(jiān)聽器
mEtUserName.addTextChangedListener(new TextWatcher() {
????????????@Override
????????????public void beforeTextChanged(CharSequence s, int start, int count, int after) {
????????????}
????????????@Override
????????????public void onTextChanged(CharSequence s, int start, int before, int count) {
????????????????Log.d("edittext",s.toString());
????????????}
????????????@Override
????????????public void afterTextChanged(Editable s) {
????????????}
????????});
七、RadioButton
<RadioGroup
????????android:id="@+id/rg1"
????????android:layout_width="wrap_content"
????????android:layout_height="wrap_content"
????????android:orientation="vertical">
android:button="@null"單選框里的按鈕樣式
android:checked="true"默認狀態(tài)下選中
android:background="@drawable/selector_orange_radiobutton"背景樣式可以自己設(shè)置例如
????????<shape>
????????????<solid android:color="#AA6600"/>
????????????<corners android:radius="20dp"/>
????????</shape>
????</item>
????<item android:state_checked="false">
????????<shape>
????????????<stroke android:width="1dp"
????????????????android:color="#aa6600"/>
????????????<corners android:radius="20dp"/>
????????</shape>
????</item>
設(shè)置監(jiān)聽器mRg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
????????????@Override
????????????public void onCheckedChanged(RadioGroup group, int checkedId) {
????????????????RadioButton radioButton =group.findViewById(checkedId);
????????????????Toast.makeText(RadioButtonActivity.this,radioButton.getText(),Toast.LENGTH_SHORT);
????????????}
????????});
八、CheckBox
基本屬性
??android:layout_below="@+id/tv_title"
????????android:layout_marginTop="10dp"
????????android:layout_width="wrap_content"
????????android:layout_height="wrap_content"
????????android:id="@+id/cb1"
????????android:text="android"
????????android:textSize="20sp"
????????android:paddingLeft="10dp"
android:button="@drawable/bg_checkbox"設(shè)置選中框的樣式狗超。在CheckBox中使用的是padding 而不是drawableLeft
例如
<selector xmlns:android="http://schemas.android.com/apk/res/android">
????<item
????????android:state_checked="false"
????????android:drawable="@drawable/uncheck"
????????/>
????<item
????????android:state_checked="true"
????????android:drawable="@drawable/check"
????????/>
</selector>
設(shè)置監(jiān)聽器
?mCb5.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
????????????@Override
????????????public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Toast.makeText(CheckBoxActivity.this,isChecked?"5選中":"5未選中",Toast.LENGTH_SHORT).show();
????????????}
????????});
????????mCb6.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
????????????@Override
????????????public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Toast.makeText(CheckBoxActivity.this,isChecked?"6選中":"6未選中",Toast.LENGTH_SHORT).show();
????????????}
????????});
????????mCb7.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
????????????@Override
????????????public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Toast.makeText(CheckBoxActivity.this,isChecked?"7選中":"7未選中",Toast.LENGTH_SHORT).show();
????????????}
????????});
????}
}
[if !supportLists]九弹澎、[endif]ImageView
基本屬性
android:layout_width="240dp"
???android:layout_height="100dp"
???android:background="#ff9900"
???android:src="@drawable/skadi"
android:scaleType="fitXY"??圖片填充類型?
fitXY撐滿控件,寬高比可能發(fā)生變化
fitCenter保持寬高比縮放努咐,直至能完全顯示
centerCrop保持寬高比縮放苦蒿,直至充滿控件,裁剪顯示
從網(wǎng)上獲取圖片并顯示:
需要先下載第三方的庫渗稍,可以通過GitHub搜索glid
點進去佩迟,通過閱讀
了解明白如何下載第三方庫,可以使用AS帶的Gradle自動安裝第三方的庫竿屹,復制當中沒有的代碼到build.gradle中报强,在上方Sync Now 便會自動下載,可能速度有點慢拱燃。
如何使用秉溉,通過閱讀
?
通過這些文檔知道了解如何使用第三方庫
例
mIv4=findViewById(R.id.iv4);Glide.with(this).load("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fa0.att.hudong.com%2F30%2F29%2F01300000201438121627296084016.jpg&refer=http%3A%2F%2Fa0.att.hudong.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1616566061&t=161ce3168283cd685b9e4aee7533f2d4").into(mIv4);
當中也許會出現(xiàn)報錯,說沒有權(quán)限碗誉,這時候我們需要到AndroidManifest.xml中添加Internet權(quán)限
<uses-permission android:name="android.permission.INTERNET"/>
十召嘶、ListView
常用屬性
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/lv1"
android:divider="@color/colorAccent"
android:dividerHeight="10dp"每個元素的間距
android:listSelector="@drawable/list_item"被選中時候的樣式
需要一個Adapter,來顯示列表的樣式哮缺,所以要新建一個class 繼承BaseAdapter
public class MyListAdapter extends BaseAdapter {????private Context mContext;????private LayoutInflater mLayoutInflater;????public MyListAdapter(Context context){????????this.mContext=context;????????mLayoutInflater=LayoutInflater.from(context);????}//給成員變量初始化值????@Override????public int getCount() {????????return 10;//列表的長度????}????@Override????public Object getItem(int position) {????????return null;????}????@Override????public long getItemId(int position) {????????return 0;????}????static class ViewHolder{????????public ImageView imageView;????????public TextView tvtitle,tvtime,tvcontent;????}????@Override????public View getView(int position, View convertView, ViewGroup parent) {????????ViewHolder holder=null;????????if (convertView==null) {????????????convertView = mLayoutInflater.inflate(R.layout.layout_list_item, null);????????????holder = new ViewHolder();????????????holder.imageView = convertView.findViewById(R.id.list_iv);????????????holder.tvtitle = convertView.findViewById(R.id.tv_titl);????????????holder.tvcontent = convertView.findViewById(R.id.tv_content);????????????holder.tvtime = convertView.findViewById(R.id.tv_time);????????????convertView.setTag(holder);????????}????????else {????????????holder= (ViewHolder) convertView.getTag();????????}????????//給控件賦值???????holder.tvtitle.setText("這是標題");????????holder.tvtime.setText("2088-88-88");????????holder.tvcontent.setText("這是內(nèi)容");????????Glide.with(mContext).load("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fa0.att.hudong.com%2F30%2F29%2F01300000201438121627296084016.jpg&refer=http%3A%2F%2Fa0.att.hudong.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1616566061&t=161ce3168283cd685b9e4aee7533f2d4").into(holder.imageView);????????return convertView;????}}
?mLv1.setAdapter(new MyListAdapter(ListViewActivity.this));????????mLv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {????????????@Override????????????public void onItemClick(AdapterView<?> parent, View view, int position, long id) {????????????????Toast.makeText(ListViewActivity.this,"pos:"+position,Toast.LENGTH_SHORT).show();????????????}????????});????}}