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

TextView的使用方法

布局文件中的配置:

在Activity类中的使用:

TextView textView1 = (TextView)findViewById(R.id.textView1);

textView1.setText(R.string.username);

EditText的使用方法

布局文件中的配置:

在Activity类中的使用:

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

Button的使用方法

布局文件中的配置:

在Activity类中的使用:

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的创建在这里执行:

@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方法:

@Override

public boolean onOptionsItemSelected(MenuItem item) {

if(item.getItemId() == 2){

finish();

}

return super.onOptionsItemSelected(item);

}

RadioGroup和RadioButton

布局文件的编写:

在Activity中使用:

RadioGroup radioGroup1 = (RadioGroup)findViewById(R.id.radioGroup1);

RadioButton radioButton1 = (RadioButton)findViewById(R.id.femaleButton);

RadioButton radioButton2 = (RadioButton)findViewById(R.id. radioButton2);

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

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

布局文件的编写:

在Activity中的使用:

CheckBox checkBox1 = (CheckBox)findViewById(R.id.checkBox1);

CheckBox checkBox2 = (CheckBox)findViewById(R.id.checkBox2);

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

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中使用:

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

ProgressBar

在布局文件中编写:

在Activity中使用:

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

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

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使用:

主布局文件的编写:

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

继承ListActivity的类

public class ActivityTest extends ListActivity {…}

在该类中生成ListView:

setContentView(R.layout.main);

ArrayList> list = new ArrayList>();

HashMapmap1 = new HashMap();

HashMapmap2 = new HashMap();

HashMapmap3 = new HashMap();

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方法:

@Override

protected void onListItemClick(ListView l, View v, int position, long id) {

super.onListItemClick(l, v, position, id);

System.out.println(id);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值