任何一款游戏来说都不会缺少UI的存在,而android自身就提供了多种UI组件,这里先介绍4种
一.按钮
最基本的控件,这个控件可以响应点击事件,在android中使用需要重写点击事件,重写的过程使用setOnClickListenr并将View.onClickListener作为参数传入,之后重写onClick方法即可
Button button=(Button)this.findViewById(R.id.start);
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
TextView textView=(TextView)ButtonDemoActivity.this.findViewById(R.id.textView1);
textView.setText("按钮示例,谢谢使用");
}
});
二.文本框
在android中可编辑文本框是EditText,仅仅用来显示文字的是TextView,TextView同样可以响应点击事件,这里用一个点击前是TextView,点击后是EditText的列子来说一下文本框,首先必须显示是TextView,再点击后在变为EditText,这个实现可以用一个折中的办法,在最开始就定义一个TextView,一个EditText,用TextView将EditText给盖住,在点击后将TextView设置为不可见,将EditText设置为可见即可,所以这里可以用framelayout的布局,这样所有组件都会显示在一个左上角,再设置EditText的android:visibility=”invisible”,这样就可以将TextView首先显示出来
效果如下
之后就是响应点击事件,事件响应方式与按钮是一致的
TextView textView=(TextView)this.findViewById(R.id.textView);
textView.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
TextView textView=(TextView)TextDemoActivity.this.findViewById(R.id.textView);
textView.setVisibility(View.INVISIBLE);//View.VISIBLE,View.INVISIBLE,View.GONE
EditText editText=(EditText)TextDemoActivity.this.findViewById(R.id.EditText);
editText.setVisibility(View.VISIBLE);
}
});
三.checkBox和RadioButton
这里使用两种不同的列子来介绍checkBox和RadioButton,实际用法可以互相替换使用
1.事先定义好CheckBox并用按钮来检测CheckBox是否选定,检测是否选定可以使用Checkbox.isChecked来判断,true为选定,反之未未选定,CheckBox.setChecked为设置选中状态,参数为true时,会将checkbox置为选中状态,反之则置为未选中
```
Button button=(Button)this.findViewById(R.id.confirm);
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
String str="";
for(int i=0;i<4;i++){
int b=0x7f080003+i;
CheckBox checkbox=(CheckBox)CheckBoxDemo.this.findViewById(b);
if(checkbox.isChecked()==true){
str=str+checkbox.getText();
}
}
str="你选择了"+str;
Toast.makeText(CheckBoxDemo.this, str, Toast.LENGTH_SHORT).show();
}
});
2.通过重写baseAdapter来定义RadioButton,Radiobutton的选定状态和CheckBox是一致的,BaseAdapter是android提供的一个基类,要使用需要重写其中的几个方法,在通过java创建类时会直接把需要重写的方法都写出来,这里就不详细写是那几个了,
关键是getView这个方法,在其中定义选中状态改变的响应事件,选中状态改变的响应事件setOnCheckedChangeListener,传入CompoundButton.OnCheckedChangeListener参数,并重写onCheckedChanged的方法
public class RadioBoxDemo extends Activity {
private ListView listview;
String []Radios={“Windows”,”linux”,”Mac Os”,”java”};//定义数组
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_radio_box_demo);
listview=(ListView)RadioBoxDemo.this.findViewById(R.id.listView1);
RadioAdapter adapter=new RadioAdapter(this);
listview.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.radio_box_demo, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
class RadioAdapter extends BaseAdapter {
Context context;
public RadioAdapter(Context context) {
this.context = context;
}
@Override
//获得需要定义的组件长度
public int getCount() {
// TODO Auto-generated method stub
return Radios.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//获得传入的context数据
RadioButton Radio= new RadioButton(context);
//设置radio的text
Radio.setText(Radios[position]);
//设置radio的id
Radio.setId(position);
Radio.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton vi,
boolean ischecked) {
// TODO Auto-generated method stub
if(vi.isChecked()==true){
if(vi.getText().toString()=="linux"){
Toast.makeText(RadioBoxDemo.this, "回答正确", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(RadioBoxDemo.this, "回答错误", Toast.LENGTH_LONG).show();
}
vi.setChecked(false);
}
}
});
return Radio;
}
}
}
“`