Android 基本控制項使用

來源:互聯網
上載者:User

標籤:

// 自動補齊的TextView

AutoCompleteTextView mPlace = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);

String[] books = new String[] { "rollen", "allen", "rollenren", "roll" };


//先定義一個未經處理資料的適配器

ArrayAdapteradapter =new ArrayAdapter<String>

(MainActivity.this,android.R.layout.simple_dropdown_item_1line,books);


//設定TextView的適配器

mPlace.setAdapter(adapter);


//輸入多個自動補齊的TextView

MultiAutoCompleteTextViewmtexts = (MultiAutoCompleteTextView)findViewById(R.id.multiAutoCompleteTextView1);


//設定TextView的適配器

mtexts.setAdapter(newArrayAdapter<String>

(MainActivity.this,android.R.layout.simple_dropdown_item_1line,books));


//設定適配器的 分割符,如逗號

mtexts.setTokenizer(newMultiAutoCompleteTextView.CommaTokenizer());


//限定輸入的字元的長度

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

text_filterred.setFilters(newInputFilter[]{

newInputFilter.AllCaps(),

newInputFilter.LengthFilter(2)

});


//Button按鈕實現Activity跳轉傳遞參數

finalButton btn = (Button)findViewById(R.id.button1);

btn.setOnClickListener(newOnClickListener(){

@Override

publicvoid onClick(View v) {

Intentintent = new Intent();

intent.setClass(MainActivity.this,Test1Activity.class);

intent.putExtra("str","come from first activity");

startActivity(intent);

}

});

 

//CheckBox 的設定及響應事件

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

check_button.setOnClickListener(newOnClickListener(){

@Override

publicvoid onClick(View v) {

tx.setText(check_button.isChecked()?"選中了":"沒選中");

}

});

 

//ToggleButton的設定及響應事件

finalToggleButton toggleButton =(ToggleButton)findViewById(R.id.toggleButton1);

toggleButton.setOnCheckedChangeListener(newOnCheckedChangeListener(){

@Override

publicvoid onCheckedChanged(CompoundButton buttonView,boolean isChecked) {

tx.setText(isChecked?"選中的":"未選中");

}

});

 

//選項按鈕RadioGroup的設定及響應事件

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


//清除選擇

group.clearCheck();

group.setOnCheckedChangeListener(newRadioGroup.OnCheckedChangeListener(){

@Override

publicvoid onCheckedChanged(RadioGroup group, int checkedId) {

if(checkedId!=-1){

RadioButtonrb =(RadioButton) findViewById(checkedId);

if(rb!=null){

tx.setText("Youchose:"+rb.getText());

}

}

}

});

 

//下拉式清單Spinner的設定及響應事件

<Spinner

android:id="@+id/spinner1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:entries="@array/colors"

android:prompt="@string/spin_prompt"/>


利用array裡面的內容指定Spinner的資料來源


spin= (Spinner) findViewById(R.id.spinner1);

spin.setOnItemSelectedListener(newOnItemSelectedListener(){

@Override

publicvoid onItemSelected(AdapterView<?> arg0, View arg1,

intarg2, long arg3) {

TextViewtvn=(TextView)arg1;//擷取其中的TextView

if(tvn!=null){

tx.setText(tvn.getText().toString());

}

}

@Override

publicvoid onNothingSelected(AdapterView<?> arg0) {

//TODO Auto-generated method stub

}

});

Android 基本控制項使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.