android學習筆記(三)基礎UI組件1——按鈕,文本框,CheckBox,Radiobutton介紹

任何一款遊戲來說都不會缺少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;




        }

    }

}
“`

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章