0%
这是一片思考的空间 -- arthinking
Spring 重构&代码整洁之道 软件设计 JVM 并发编程 数据结构与算法 分布式 存储 网络 微服务 设计模式
Java技术栈 - 涉及Java技术体系

Android中的常用控件及其基本用法

TextView的使用方法

布局文件中的配置:

1
2
3
4
5
6
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/arthinking"
/>

在Activity类中的使用:

1
2
TextView textView1 = (TextView)findViewById(R.id.textView1);
textView1.setText(R.string.username);

EditText的使用方法

布局文件中的配置:

1
2
3
4
5
<EditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>

在Activity类中的使用:

1
EditText editText1  = (EditText)findViewById(R.id.editText1);

Button的使用方法

布局文件中的配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
```xml

**在Activity类中的使用:**

```java
Button button1 = (Button)findViewById(R.id. button1);
//TestListener为继承OnClickListener的类
button1.setOnClickListener(new TestListener());

Menu的使用方法

onCreateOptionsMenu(Menu menu) Initialize the contents of the Activity's standard options menu. onCreateOptionsMenu是Activity中的一个方法,当用户点击了MENU按钮时,Activity会触发该方法,Menu的创建在这里执行:

1
2
3
4
5
6
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, 1, 1, R.string.back);
menu.add(0,2,2,R.string.exit);
return super.onCreateOptionsMenu(menu);
}

为按钮添加方法,需要实现Activity的onOptionsItemSelected方法:

1
2
3
4
5
6
7
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == 2){
finish();
}
return super.onOptionsItemSelected(item);
}

RadioGroup和RadioButton

布局文件的编写:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/female"
/>
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/male"
/>
</RadioGroup>

在Activity中使用:

1
2
3
RadioGroup radioGroup1 = (RadioGroup)findViewById(R.id.radioGroup1);
RadioButton radioButton1 = (RadioButton)findViewById(R.id.femaleButton);
RadioButton radioButton2 = (RadioButton)findViewById(R.id. radioButton2);

为RadioGroup设置监听器,使用RadioGroup.OnCheckedChangeListener类

1
2
3
4
5
6
7
8
9
10
11
12
13
radioGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(radioButton1.getId() == checkedId){
System.out.println("radioButton1");
}
else if(radioButton2.getId() == checkedId)
{
System.out.println("radioButton2");
}
}
});

CheckBox

布局文件的编写:

1
2
3
4
5
6
7
8
9
10
11
12
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/checkBox1"
/>
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/checkBox2"
/>

在Activity中的使用:

1
2
CheckBox checkBox1 = (CheckBox)findViewById(R.id.checkBox1);
CheckBox checkBox2 = (CheckBox)findViewById(R.id.checkBox2);

为多选按钮添加监听器,这里使用CompoundButton.OnCheckedChangeListener

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked)
{
System.out.println("checkBox1 is checked");
}
else
{
System.out.println("checkBox1 is unchecked");
}
}
});
checkBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked)
{
System.out.println("checkBox2 is checked");
}
else
{
System.out.println("checkBox2 is unchecked");
}
}
});

Toast

public class Toast extends Object A toast is a view containing a quick little message for the user. The toast class helps you create and show those.

直接在Activity中使用:

1
Toast.makeText(RadioTest.this, "checkBox1", Toast.LENGTH_SHORT).show();

ProgressBar

在布局文件中编写:

1
2
3
4
5
6
7
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:visibility="gone"
/>

在Activity中使用:

1
ProgressBar progressBar1 = (ProgressBar)findViewById(R.id.progressBar1);

这里使用一个鼠标点击事件触发处理该进度条:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
private int i = 0;
class ButtonListener implements OnClickListener{

@Override
public void onClick(View v) {
if(i == 0) {
progressBar1.setVisibility(View.VISIBLE);
progressBar1.setMax(100);
}
else if ( i < progressBar1.getMax()){
progressBar1.setProgress(i);
progressBar1.setSecondaryProgress(i + 10);
}
else{
progressBar1.setVisibility(View.GONE);
}
i = i + 10 ;
}
}

ListView

public class ListView extends AbsListView A view that shows items in a vertically scrolling list. The items come from the ListAdapter associated with this view.

要使用ListView必须继承ListActivity类,同时需要在代码中构造 一个android.widget.SimpleAdapter类,用于辅助创建ListView。除此之外还需要创建另外一个布局文件供ListView使用:

主布局文件的编写:

1
2
3
<ListView android:id="@id/android:list" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:drawSelectorOnTop="true"
android:scrollbars="vertical" />

另外需要一个布局文件供ListView使用:

1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="horizontal" android:paddingLeft="10dip"
android:paddingRight="10dip" android:paddingTop="2dip"
android:paddingBottom="2dip">
<TextView android:id="@+id/user_name" android:layout_width="180dip"
android:layout_height="30dip" android:textSize="5pt"
android:singleLine="true" />
<TextView android:id="@+id/user_id" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:gravity="right"
android:textSize="5pt" />
</LinearLayout>

继承ListActivity的类

1
public class ActivityTest extends ListActivity {…}

在该类中生成ListView:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
setContentView(R.layout.main);
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map1 = new HashMap<String, String>();
HashMap<String, String> map2 = new HashMap<String, String>();
HashMap<String, String> map3 = new HashMap<String, String>();
map1.put("user_name", "arthinking");
map1.put("user_id", "001");
map2.put("user_name", "Jason");
map2.put("user_id", "002");
list.add(map1);
list.add(map2);
SimpleAdapter listAdapter = new SimpleAdapter(this, list,
R.layout.user, new String[]{"user_name","user_id"},
new int[]{R.id.user_name,R.id.user_id});
setListAdapter(listAdapter);

要实现监听事件,可以实现ListActivity的onListItemClick方法:

1
2
3
4
5
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
System.out.println(id);
}

欢迎关注我的其它发布渠道

订阅IT宅
内功修炼
Java技术栈
Java架构杂谈是IT宅精品文章公众号,欢迎订阅:
📄 网络基础知识:两万字长文50+张趣图带你领悟网络编程的内功心法 📄 HTTP发展史:三万长文50+趣图带你领悟web编程的内功心法 📄 HTTP/1.1:可扩展,可靠性,请求应答,无状态,明文传输 📄 HTTP/1.1报文详解:Method,URI,URL,消息头,消息体,状态行 📄 HTTP常用请求头大揭秘 📄 HTTPS:网络安全攻坚战 📄 HTTP/2:网络安全传输的快车道 📄 HTTP/3:让传输效率再一次起飞 📄 高性能网络编程:图解Socket核心内幕以及五大IO模型 📄 高性能网络编程:三分钟短文快速了解信号驱动式IO 📄 高性能网络编程:彻底弄懂IO复用 - IO处理杀手锏,带您深入了解select,poll,epoll 📄 高性能网络编程:异步IO:新时代的IO处理利器 📄 高性能网络编程:网络编程范式 - 高性能服务器就这么回事 📄 高性能网络编程:性能追击 - 万字长文30+图揭秘8大主流服务器程序线程模型
📄 Java内存模型:如果有人给你撕逼Java内存模型,就把这些问题甩给他 📄 一文带你彻底理解同步和锁的本质(干货) 📄 AQS与并发包中锁的通用实现 📄 ReentrantLock介绍与使用 📄 ReentrantReadWriteLock介绍与使用 📄 ReentrantLock的Condition原理解析 📄 如何优雅的中断线程 📄 如何优雅的挂起线程 📄 图解几个好玩的并发辅助工具类 📄 图解BlockingQueue阻塞队列
📄 消息队列那么多,为什么建议深入了解下RabbitMQ? 📄 高并发异步解耦利器:RocketMQ究竟强在哪里? 📄 Kafka必知必会18问:30+图带您看透Kafka
📄 洞悉MySQL底层架构:游走在缓冲与磁盘之间 📄 SQL运行内幕:从执行原理看调优的本质 📄 洞悉Redis技术内幕:缓存,数据结构,并发,集群与算法