天天看点

移动测试之AutoCompleteTextView、ToggleButton

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:开关按钮

(一)、类结构:

   ↳ android.view.View

    ↳ android.widget.TextView

      ↳ android.widget.Button

        ↳ android.widget.CompoundButton

          ↳ android.widget.ToggleButton

(二)、UI核心代码:

<ToggleButton

android:id="@+id/toggleButton_main_one"

android:layout_width="wrap_content"

android:textOn="开"

android:textOff="关"/>

android:id="@+id/toggleButton_main_two"

private ToggleButton toggleButton_main_one;

private ToggleButton toggleButton_main_two;

toggleButton_main_one = (ToggleButton) findViewById(R.id.toggleButton_main_one);

toggleButton_main_two = (ToggleButton) findViewById(R.id.toggleButton_main_two);

OnCheckedChangeListener listener = new OnCheckedChangeListener() {

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);