seekbar不在Dialog自定义布局中更新textview

时间:2016-05-28 18:35:49

标签: android seekbar

我有一个搜索栏的布局和我在对话框中使用的文本视图。 当我尝试从搜索栏更新textview时没有任何反应。

我正在onProgressChanged中进行更改。 以下是 milage_main.class:

中的代码
public class milage_spinner extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

SeekBar seekBar = (SeekBar)findViewById(R.id.seekBar);
seekBar.setProgress(10);
seekBar.setMax(10);

final TextView sbv = (TextView) findViewById(R.id.driveTime);
final EditText drive_time = (EditText)findViewById(R.id.runTime);

seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
sbv.setText(String.valueOf(progress));
drive_time.setText(progress);
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
}

这是 MainActiviy.class 中的对话框构建器,它正在获取我的布局 milage_seekbar

private Dialog createMilage(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
LinearLayout linearLayout = (LinearLayout)inflater.inflate(R.layout.milage_spinner,null);
builder.setView(linearLayout)
.setTitle("Ktd")
.setNegativeButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int i) {
dialog.dismiss();
}
})
;
return builder.create();
}

我在布局milage_seekbar中的XML看起来像这样:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/driveTime"
android:layout_gravity="center_horizontal" />
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/seekBar"
android:layout_gravity="center_horizontal" />
</LinearLayout>

是否可以在主要活动中的对话框中设置 onProgressChanged

1 个答案:

答案 0 :(得分:1)

milage_spinner xml代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="2dp"
    android:layout_gravity="center" >

    <TextView
        android:id="@+id/set_size_help_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Please select snowflake size"

        android:textSize="25dp" />

    <SeekBar
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/size_seekbar"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp" />

</LinearLayout>

在java代码中将对话框更改为AlertDialog并调用oncreate。

 private AlertDialog createMilage() {
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.milage_spinner, (ViewGroup) findViewById(R.id.layout));
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .setView(layout);
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        AlertDialog alertDialog = builder.create();

        alertDialog.show();
        SeekBar sb = (SeekBar) layout.findViewById(R.id.size_seekbar);
        final TextView set_size_help_text = (TextView) layout.findViewById(R.id.set_size_help_text);
        sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                set_size_help_text.setText("" + progress);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        return alertDialog;
    }

enter image description here