• Android -- 对话框 Dialog: 确定取消,单选多选,进度显示, 常用控件示例:单选多选框,下拉列表,进度条,自动完成文本框, 菜单显示


    public class MainActivity extends Activity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    }
    
    /**
     * 确定取消对话框
     * 
     * @param view
     */
    public void click1(View view) {
    // 1.创建一个对话框的构造器.
    AlertDialog.Builder builder = new Builder(this);
    builder.setTitle("java培训标题");
    builder.setIcon(R.drawable.ic_launcher);
    builder.setMessage("想打开官方网站么?");
    builder.setPositiveButton("确定", new OnClickListener() {
    
    @Override
    public void onClick(DialogInterface dialog, int which) {
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_VIEW);
    intent.setData(Uri.parse("http://edu.csdn.net"));
    startActivity(intent);
    }
    });
    builder.setNegativeButton("取消", new OnClickListener() {
    
    @Override
    public void onClick(DialogInterface dialog, int which) {
    // 默认实现, 点击后 关闭对话框
    }
    });
    
    // builder.create().show();
    builder.show();
    
    }
    
    /**
     * 单选对话框
     * 
     * @param view
     */
    public void click2(View view) {
    // 1.创建一个对话框的构造器.
    AlertDialog.Builder builder = new Builder(this);
    builder.setTitle("你想吃什么");
    final String[] items = new String[] { "苹果", "葡萄", "香蕉" };
    builder.setSingleChoiceItems(items, 1, new OnClickListener() {
    
    @Override
    public void onClick(DialogInterface dialog, int which) {
    Toast.makeText(MainActivity.this, "您选择了吃" + items[which], 0)
    .show();
    dialog.dismiss();
    }
    });
    
    // builder.create().show();
    builder.show();
    
    }
    
    /**
     * 多选对话框
     * 
     * @param view
     */
    public void click3(View view) {
    // 1.创建一个对话框的构造器.
    AlertDialog.Builder builder = new Builder(this);
    builder.setTitle("你想吃什么");
    final String[] items = new String[] { "苹果", "葡萄", "香蕉" };
    final boolean[] status = new boolean[] { true, false, true };
    builder.setMultiChoiceItems(items, status,
    new OnMultiChoiceClickListener() {
    
    @Override
    public void onClick(DialogInterface dialog, int which,
    boolean isChecked) {
    if (isChecked) {
    Toast.makeText(MainActivity.this,
    items[which] + "被选中了", 0).show();
    
    } else {
    Toast.makeText(MainActivity.this,
    items[which] + "被取消选中了", 0).show();
    }
    status[which] = isChecked;
    }
    });
    
    builder.setPositiveButton("确定", new OnClickListener() {
    
    @Override
    public void onClick(DialogInterface dialog, int which) {
    // todo:遍历数组 查看状态
    
    }
    });
    
    // builder.create().show();
    builder.show();
    
    }
    
    /**
     * 进度条对话框
     * 
     * @param view
     */
    public void click4(View view) {
    ProgressDialog pd = new ProgressDialog(this);
    pd.setTitle("请稍后");
    pd.setMessage("正在后台处理...");
    pd.show();
    new Thread(){
    public void run(){
    Thread.sleep(2000);
    pd.dismiss();
    }
    }.start();
    
    }
    /**
     * 显示进度的进度条对话框
     * 
     * @param view
     */
    public void click5(View view) {
    final ProgressDialog pd = new ProgressDialog(this);
    pd.setTitle("请稍后");
    pd.setMessage("正在后台处理...");
    pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    pd.setMax(100);
    new Thread(){
    public void run() {
    for(int i = 0;i<100;i++){
    pd.setProgress(i);
    try {
    Thread.sleep(30);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    pd.dismiss();
    };
    }.start();
    pd.show();
    }
    }

    2. 单选框(RadioButton)

    转存失败重新上传取消

    要完成单选框显示,我们需要使用到RadioGroup和RadioButton(单选框),RadioGroup用于对单选框进行分组,相同组内的单选框只有一个单选框能被选中。(例子代码请见下方备注栏)

     RadioGroup.check(R.id.dotNet);将id名为dotNet的单选框设置成选中状态。 (RadioButton) findViewById(radioGroup.getCheckedRadioButtonId());//获取被选中的单选框。 RadioButton.getText();//获取单选框的值 调用setOnCheckedChangeListener()方法,处理单选框被选择事件,把RadioGroup.OnCheckedChangeListener实例作为参数传入

    3. 多选框(CheckBox)

    每个多选框都是独立的,可以通过迭代所有多选框,然后根据其状态是否被选中再获取其值。

    CheckBox.setChecked(true);//设置成选中状态。 CheckBox.getText();//获取多选框的值  调用setOnCheckedChangeListener()方法,处理多选框被选择事件,把CompoundButton.OnCheckedChangeListener实例作为参数传入   4. 下拉列表框(Spinner) 转存失败重新上传取消

    public class MainActivity extends Activity {
    
    protected static final String TAG = "MainActivity";
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    // 第二个参数为下拉列表框每一项的界面样式,该界面样式由Android系统提供,当然您也可以自定义
    ArrayAdapter adapter = new ArrayAdapter(this,
    android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    adapter.add("java");
    adapter.add("dotNet");
    adapter.add("php");
    
    Spinner spinner = (Spinner) findViewById(R.id.spinner);
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    
    @Override
    public void onItemSelected(AdapterView parent, View view,
    int position, long id) {
    Spinner spinner = (Spinner)parent;
    String itemContent = (String)parent.getItemAtPosition(position);
    Toast.makeText(getApplicationContext(), itemContent, 0).show();
    
    }
    
    @Override
    public void onNothingSelected(AdapterView parent) {
    Log.i(TAG,  parent.getClass().getName());
    }
    
    });
    
    }
    }
    


    5. 拖动条(SeekBar)  SeekBar.getProgress()获取拖动条当前值 调用setOnSeekBarChangeListener()方法,处理拖动条值变化事件,把SeekBar.OnSeekBarChangeListener实例作为参数传入 6. 进度条(ProgressBar)

    在布局xml文件中添加进度条代码:

        android:layout_width="fill_parent"

        android:layout_height="20px"

        style="?android:attr/progressBarStyleHorizontal"

        android:id="@+id/downloadbar"/>

    在代码中操作进度条:

    ProgressBar.setMax(100);//设置最大刻度

    ProgressBar.setProgress(0);//设置进度条的当前刻度,如果进度条的最大刻度为100,当前刻度为50,进度条将进行到一半。

    7. 输入内容自动完成文本框(AutoCompleteTextView)

       android:layout_width="fill_parent“  android:layout_height="wrap_content“

      

       android:completionThreshold="1“ ,

       android:id="@+id/name" />

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    String[] names = {"老张", "老方", "老毕", "李明" , "李丽", "陈江", "abc", "acc"};
    AutoCompleteTextView nameText = (AutoCompleteTextView)this.findViewById(R.id.actv);
    ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, names);
    nameText.setAdapter(adapter);
    
    }


    8. 多次输入-内容自动完成文本框(MultiAutoCompleteTextView)

    除了AutoCompleteTextView控件外,我们还可以使用MultiAutoCompleteTextView控件来完成连续输入的功能。也就是说,当输入完一个字符串后,在该字符串后面输入一个逗号(,),在逗号前后可以有任意多个空格,然后再输入一个字符串,仍然会显示自动提示列表。

    使用MultiAutoCompleteTextView时,需要为它的setTokenizer方法指定MultiAutoCompleteTextView.CommaTokenizer类对象实例,

    该对象表示采用逗号作为输入多个字符串的分隔符。

    MultiAutoCompleteTextView

       android:layout_width="fill_parent“  android:layout_height="wrap_content“

      

       android:completionThreshold="1“ 

       android:id="@+id/name" />

    public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

      setContentView(R.layout.main);

         String[] names = {"老张", "老方", "老毕", "李明" , "李丽", "陈江", "abc", "acc"};

         MultiAutoCompleteTextView nameText = (MultiAutoCompleteTextView)this.findViewById(R.id.name);

         ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line,    names);

         nameText.setAdapter(adapter);

         nameText.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());}


    9. 菜单(Menu)

    /res/memu/activity_main.xml  菜单的布局文件

    
    
        
        
        
        
    
    

    MainActivity.java 菜单的创建和点击响应

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.item1:
    Toast.makeText(this, "菜单条目1被点击了", 0).show();
    break;
    
    case R.id.item2:
    Toast.makeText(this, "菜单条目2被点击了", 0).show();
    break;
    }
    
    return super.onOptionsItemSelected(item);
    }
  • 相关阅读:
    Linux基础命令[25]-groupadd
    c++day6---9.13
    R 语言学习教程,从入门到精通,R 基础运算(5)
    【接口测试】测试基础
    2022 RoboCom 世界机器人开发者大赛-本科组(省赛)-RC-u5 树与二分图
    Taurus.MVC WebAPI 入门开发教程3:路由类型和路由映射。
    Go1.9.3跑GinDemo
    参数估计之区间估计(统计知识)以及Python代码
    python多进程编程
    短视频如何批量添加水印
  • 原文地址:https://blog.csdn.net/cqn2bd2b/article/details/126435242