AutoCompleteTextView:自動完成文本框
(一)、介紹:
自動完成文本框,用於實現用戶輸入一定字符後,顯示一個下拉式菜單,讓用戶從中選擇,當用戶選擇後,就會自動填寫該文本框。
類結構:
java.lang.Object
↳ android.view.View
↳ android.widget.TextView
↳ android.widget.EditText
↳ android.widget.AutoCompleteTextView
(二)、xml常用屬性:
1、android:completionThreshold 用於指定用戶至少輸入幾個字符纔會顯示提示
UI核心代碼:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:completionThreshold="1"
android:completionHint="請輸入:"
android:ems="10"
android:text="" >
<requestFocus />
</AutoCompleteTextView>
</LinearLayout>
(三)、java核心代碼:
public class MainActivity extends Activity {
private AutoCompleteTextView autoCompleteTextView_main;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
autoCompleteTextView_main = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView_main);
String[] arrStr = new String[] { "about", "adapter", "apple",
"android", "angle", "angel" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
MainActivity.this, android.R.layout.simple_dropdown_item_1line,
arrStr);
autoCompleteTextView_main.setAdapter(adapter);
}
}
三、ToggleButton:開關按鈕
(一)、類結構:
java.lang.Object
↳ android.view.View
↳ android.widget.TextView
↳ android.widget.Button
↳ android.widget.CompoundButton
↳ android.widget.ToggleButton
(二)、UI核心代碼:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ToggleButton
android:id="@+id/toggleButton_main_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="開"
android:textOff="關"/>
<ToggleButton
android:id="@+id/toggleButton_main_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="開"
android:textOff="關"/>
</LinearLayout>
(三)、java核心代碼:
public class MainActivity extends Activity {
private ToggleButton toggleButton_main_one;
private ToggleButton toggleButton_main_two;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toggleButton_main_one = (ToggleButton) findViewById(R.id.toggleButton_main_one);
toggleButton_main_two = (ToggleButton) findViewById(R.id.toggleButton_main_two);
OnCheckedChangeListener listener = new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
String result1 = toggleButton_main_one.isChecked() ? "開" : "關";
String result2 = toggleButton_main_two.isChecked() ? "開" : "關";
setTitle(result1 + ":" + result2);
}
};
toggleButton_main_one.setOnCheckedChangeListener(listener);
toggleButton_main_two.setOnCheckedChangeListener(listener);
}
}
編輯:千鋒軟件測試