四、4-2常见控件的使用方法

控件通用属性:

android:id=“@+id/id1”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”

1、TextView

android:text=“hello world!”
android:gravity=“center” (top | bottom | left | right | center | center_vertical | center_horizontal) 文字的对齐方式
android:textSize=“24sp”
android:textColor=“#00ff00”

2、Button

android:layout_margin=“5dip” --》指定控件在上下左右方向上偏移的距离
android:layout_marginLeft=”5dp"
android:layout_marginRight=”5dp"
android:layout_marginTop=”5dp"
android:layout_marginBottom=”5dp"
android:background=“@drawable/back_bg”
为Button的点击事件注册监听器

button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
	@Override
	public void onClick(View v) {
		// 在此处添加逻辑
	}
});
public class MainActivity extends Activity implements OnClickListener {
	private Button button;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		button = (Button) findViewById(R.id.button);
		button.setOnClickListener(this);
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.button:
				// 在此处添加逻辑
				break;
			default:
				break;
		}
	}
}

3、EditText

android:hint=“Type something here”
android:maxLines=“2” 最大行数为两行
JAVA:
editText.getText().toString() --》getText()方法获取到输入的内容

4、ImageView

android:src=“@drawable/ic_launcher”
imageView.setImageResource(R.drawable.jelly_bean); --》setImageResource()方法改变显示图片

5、ProgressBar

android:visibility=“visible” (invisible gone)
style=“?android:attr/progressBarStyleHorizontal” --》通过 style 属性可以将它指定成水平进度条
android:max=“100” --》通过 android:max 属性给进度条设置一个最大值
JAVA:
progressBar.setVisibility(View.VISIBLE);

6、AlertDialog

AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
dialog.setTitle("This is Dialog");
dialog.setMessage("Something important.");
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 which) {
	}
});
dialog.show();

7、ProgressDialog

ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setTitle("This is ProgressDialog");
progressDialog.setMessage("Loading...");
progressDialog.setCancelable(true);
progressDialog.show();
注意如果在 setCancelable()中传入了 false,表示 ProgressDialog 是不能通过 Back 键取消掉的,
这时你就一定要在代码中做好控制,当数据加载完成后必须要调用 ProgressDialogdismiss()方法来关闭对话框,
否则 ProgressDialog 将会一直存在。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值