常用控件
1. CheckBox(复选框)
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); checkBox = (CheckBox) findViewById(R.id.checkbox_id); checkBox.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(checkBox.isChecked()) Toast.makeText(MainActivity.this,"CheckBox选中", Toast.LENGTH_SHORT).show(); else Toast.makeText(MainActivity.this,"CheckBox未选中", Toast.LENGTH_SHORT).show(); } }); }
2. RadioGroup/RadioButton(单选按钮)
参考:https://www.cnblogs.com/wt616/archive/2011/06/20/2085531.html
RadioButton表示单个圆形单选框,而RadioGroup是可以容纳多个RadioButton的容器
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); radioGroup = findViewById(R.id.radioGroup_id); radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup radioGroup, int i) { RadioButton radbtn = (RadioButton) findViewById(i); Toast.makeText(getApplicationContext(), "按钮组值发生改变,你选了" + radbtn.getText(), Toast.LENGTH_LONG).show(); } }); }
3. SeekBar(拖动条)
参考资料:http://www.runoob.com/w3cnote/android-tutorial-seekbar.html
<SeekBar android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" android:progress="50" android:id="@+id/seekbar_id" />
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); seekBar = findViewById(R.id.seekbar_id); tv = findViewById(R.id.textView_id); seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean b) { tv.setText("当前进度值:" + progress + "/100 "); } @Override public void onStartTrackingTouch(SeekBar seekBar) { Toast.makeText(MainActivity.this, "触碰SeekBar", Toast.LENGTH_SHORT).show(); } @Override public void onStopTrackingTouch(SeekBar seekBar) { Toast.makeText(MainActivity.this, "放开SeekBar", Toast.LENGTH_SHORT).show(); } }); }
4. ScrollView(滚动条)
参考资料:http://www.runoob.com/w3cnote/android-tutorial-scrollview.html
注意:ScrollView是FrameLayout的子类,并不是widget控件的子类,并不是控件,是Framlayout容器,只是功能类似控件,放到这里
java.lang.Object
android.view.View
android.view.ViewGroup
android.widget.FrameLayout
android.widget.ScrollView
<Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/scrollButtonup_id" android:text="上"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/scrollButtondown_id" android:text="下"/> <ScrollView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:id="@+id/scrollView_id"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/textView_id"/> </ScrollView>
{ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView) findViewById(R.id.textView_id); sl = (ScrollView) findViewById(R.id.scrollView_id); down = (Button) findViewById(R.id.scrollButtondown_id); up = (Button)findViewById(R.id.scrollButtonup_id); StringBuilder str1 = new StringBuilder(); for(int ii=0; ii<100; ii++){ str1.append("ceshi"+ii+"\n"); } tv.setText(str1.toString()); up.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { sl.fullScroll(View.FOCUS_UP ); } }); down.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { sl.fullScroll(View.FOCUS_DOWN ); } }); }
5. AlertDialog(对话框)
public class MainActivity extends Activity implements View.OnClickListener{ Button bt_normal; Button bt_single; Button bt_multi; Button bt_speed; AlertDialog alertDialog; AlertDialog.Builder builder; Context context; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bt_normal = (Button) findViewById(R.id.normal_id); bt_single = (Button) findViewById(R.id.single_id); bt_multi = (Button) findViewById(R.id.multi_id); bt_speed = (Button) findViewById(R.id.speed_id); bt_normal.setOnClickListener(this); bt_single.setOnClickListener(this); bt_multi.setOnClickListener(this); bt_speed.setOnClickListener(this); context = MainActivity.this; } //多选框的两个参数 String[] str1 = {"小李", "小张", "小王", "小陈", "小丽"}; boolean[] bool1 = {true,false, true, false, true}; @Override public void onClick(View v) { switch (v.getId()){ case R.id.normal_id: builder = new AlertDialog.Builder(context); builder.setTitle("普通对话框的标题"); builder.setMessage("普通对话框的信息"); builder.setPositiveButton("确认键", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(context, "你已选择确定键", Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("取消键", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(context, "你已选择取消键", Toast.LENGTH_SHORT).show(); } }); builder.show(); break; case R.id.single_id: final String[] str = {"北京", "上海", "深圳", "台北", "长沙"}; builder = new AlertDialog.Builder(context); builder.setSingleChoiceItems(str, 2, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(context, "你选择了"+str[which], Toast.LENGTH_SHORT).show(); } }); builder.show(); break; case R.id.multi_id: builder = new AlertDialog.Builder(context); builder.setMultiChoiceItems(str1, bool1, new OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { if(isChecked==true) Toast.makeText(context, "你选择了"+str1[which], Toast.LENGTH_SHORT).show(); else Toast.makeText(context, "你取消了"+str1[which], Toast.LENGTH_SHORT).show(); } }); builder.show(); break; case R.id.speed_id: final ProgressDialog dialog = ProgressDialog.show(this, null, "姐们,正在拼命 加载中...."); //让 在子线程中睡 3 秒钟 new Thread(){ public void run() { SystemClock.sleep(3000); dialog.dismiss(); Log.d("MainActivity", "子线程"); }; }.start(); Log.d("MainActivity", "主线程"); break; } Log.d("MainActivity", "onClick after"); } }
普通对话框
单选框
多选框
进度