android(常用控件)-9

本文介绍了 Android 开发中常用的 UI 控件及其基本属性与方法,包括 TextView、Button、EditText、ImageView 和 ProgressBar 等,并提供了如何创建自定义控件及布局复用的方法。

    控件,这东西我感觉是最重要的东西,我还记得以前学C的时候最在的想法就是写个界面,那时候窗口就是一个cmd窗口,完全不知道怎么做一个界面,后来学用QT写C++的时候才知道有控件这回事但好像不是很多,后来用VS时,写c++,c#,拖控件拖得好爽。android里面也有很多的控件,但大多数人好像都喜欢用代码来搞。我想先学几个常用的玩玩吧,太高深的用到的再慢慢研究。

常用的属性:

android:id

android:layout_height   高度

android:layout_width     宽度

android:gravity 对齐方式

android:orientation 布局

常用函数 :

findViewById:   如button = (Button)findViewById(R.id.button);      


TextView:

最先接触的叫hello world程序


Button:按键 

要用到的监听  button.setOnClickListener(new OnClikListernr(){@Override public void onClick(View v){} })

匿名  监听 button.setOnClickListener(this)  @override public void onClick(View v){ switck(v.getId()) case R.id.button break;}


EditText:

常用属性:maxlines

常用函数:getText(); setText();  如String intputText = editText.getText().toString();


ImageView图片:

常用属性src (把资源图片放入drawble 然后build才会在目录中显现出来)

常用函数:setImageResource()


ProgressBar进度条:

常用属性:style="android:attr/progressBarStyleHorizontal"

常用函数:getVisibility()  setVisibility() View.VISIBLE View.GONE 可见状态 getProgress(),setProgress()设置、获取进度


AlterDialog单出对话框:

使用方法

AlertDialog.Builder dialog = new AlertDialog.Builder(FirstActivity.this);

dialog.setTitle("弹出消息框");

dialog.setMessage("通知信息");

dialog.setCancelable(false);

dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener(){
    @Override
    public void onClick(DialogInterface dialog, int which){

    }
});

dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
    @Override 
    public void onClick(DialogInterface dialog, int whick){
        
    }
});

dialog.show();


ProgressDialog:与AlertDialog相像

ProgressDialog progressDialog = new ProgressDialog(FirstActivity.this);
progressDialog.setTitle("有个进度条哦");
progressDialog.setMessage("Loading...");
progressDialog.setCancelable(true);
progressDialog.show();


引用布局:

引用布局来解决布局代码重复的问题,说白了就的布局里面布一个布好的局嘛。

我们如果有个title.xml文件如果我们使用了话包进去就好了

<include layout="@layout/title"></include>

创建自定义控件:

创建自定义控件可解决注册代码重复的问题

public class TitleLayout extends LinearLayout {
  public TitleLayout(Context context, AttributeSet attrs){
      super(context, attrs);
      LayoutInflater.from(context).inflate(R.layout.title, this);
  }
}
添加的时候就当普通控件用

<com.ouyang.uicustomviews.TitleLayout>  
    adroid:layout_width="wrap_content"
    android:layout_height="wrap_content"
</com.ouyang.uicustomviews.TitleLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值