结果展示:

输入20是,进图条进度实时更改为20
进度条进度实时更改为80

关键代码部分

xml:

activity_progressbar_test.xml

 <ProgressBar style="@android:style/Widget.ProgressBar.Horizontal"
        android:id="@+id/pgBar5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="20"
        android:progressTint="@color/colorAccent"
        android:secondaryProgress="80"
        android:secondaryProgressTint="@color/colorRed"
        android:background="@color/colorGreen"
        />


    <EditText
        android:id="@+id/txtProgress"
        android:text="20"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

java:
ProgressbarTestActivity.java

EditText txtProgress;
ProgressBar pgBar5;




pgBar5=(ProgressBar)findViewById(R.id.pgBar5);
        txtProgress=(EditText)findViewById(R.id.txtProgress);




   txtProgress.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }
            @Override
            public void afterTextChanged(Editable s) {
                if(!txtProgress.getText().toString().trim().equals("")) {
                    int num = Integer.parseInt(txtProgress.getText().toString());
                    pgBar5.setProgress(num);
                }
             }
        });
       

代码分析:

1、txtProgress.addTextChangedListener(new TextWatcher() {

通过为EditText组件设置文件内容更改的监听事件来实时监听用户的在组件中的输入

2、trim() 函数用来去除字符串两边的空格

3、txtProgress.getText()函数

用来获取EditText组件中用户输入的字符

4、pgBar5.setProgress(Int i)

将进度条的当前进度设置为i

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐