1

首先,我是 Android 开发新手,所以不要对我这么苛刻。

我试图让 SeekBar 调整应用程序或整个系统的亮度级别。虽然,我无法让它发挥作用。

我的 XML 文件如下所示:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".SettingsPage" >

<SeekBar
    android:id="@+id/seekBar2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/seekBar"
    android:layout_below="@+id/seekBar"
    android:layout_marginTop="30dp" />

<SeekBar
    android:id="@+id/seekBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="21dp"
    android:max="100"
    android:progress="50" />

</RelativeLayout>

MainActivity 的 XML 代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<Button
    android:id="@+id/buttonSettings"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:text="Button" 
    android:onClick="settingsPage"/>

</RelativeLayout>

XML 代码,清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testsetup"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-sdk

    android:minSdkVersion="14"
    android:targetSdkVersion="18" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.testsetup.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.testsetup.SettingsPage"
        android:label="@string/title_activity_settings_page" >
    </activity>
    <activity
        android:name="com.example.testsetup.BrightnessAdjuster"
        android:label="@string/title_activity_brightness_adjuster" >
    </activity>
    <activity
        android:name="com.example.testsetup.BrightnessActivity"
        android:label="@string/title_activity_brightness" >
    </activity>
</application>

</manifest>

我拥有的 Java 代码如下所示:

   import android.app.Activity;
   import android.os.Bundle;
   import android.view.Menu;
   import android.view.View;
   import android.view.WindowManager;
   import android.widget.SeekBar;
   import android.widget.SeekBar.OnSeekBarChangeListener;
   import android.widget.TextView;

    public class SettingsPage extends Activity {

float BackLightValue = 0.5f; // dummy default value


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings_page);
    SeekBar BackLightControl = (SeekBar) findViewById(R.id.action_settings);

    BackLightControl
            .setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener());
}

public void onClick(View arg0) {
    int SysBackLightValue = (int) (BackLightValue * 255);
    android.provider.Settings.System.putInt(getContentResolver(),
            android.provider.Settings.System.SCREEN_BRIGHTNESS,
            SysBackLightValue);

}

public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
    BackLightValue = (float) arg1 / 100;
    WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
    layoutParams.screenBrightness = BackLightValue;
    getWindow().setAttributes(layoutParams);
}

public void onStartTrackingTouch(SeekBar arg0) {

}

public void onStopTrackingTouch(SeekBar arg0) {

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.settings_page, menu);
    return true;
}

}

MainActivity 的 Java 代码

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
public void settingsPage(View view){
    Intent intent = new Intent(this, SettingsPage.class);
    startActivity(intent);

}

}

问题是我不知道这是否是正确的方法,即使我已经尝试遵循关于此事的几个指南。

另外,我在上面的代码中遇到一个错误,它是以下行

BackLightControl
        .setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener());

I get the error that "cannot instantiate type SeekBar.OnSeekBarChangeListener()". I thought it was due to the fact that OnSeekBarChangeListener was an interface, so I tried to create a random class that implemented OnSeekBarChangeListener and instantiate that class, but that did not work either.

So, now is my question; how to proceed from here?

Regards and thanks in advance

Erik

EDIT

Added my other classes and XML files. Plus, I no longer get the error on the line with instantiating the OnSeekBarChangeListener thing.

Regards

Erik

4

1 回答 1

0

You cannot in instantiate OnSeekBarChangeListener because it's an interface. If you create a class which implements this interface, and use an instance of that class as the parameter of setOnSeekBarChangeListener, that should definitely work. The easiest way is letting your Activity implement OnSeekBarChangeListener, and pass this to the setter method.

public class SettingsPage extends Activity implements SeekBar.OnSeekBarChangeListener {

    // ...

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

        setContentView(R.layout.activity_settings_page);
        SeekBar BackLightControl = (SeekBar) findViewById(R.id.action_settings);

        BackLightControl.setOnSeekBarChangeListener(this);
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        // ...
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        // ...
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        // ...
    }

    // ...

}

Side note: you should use standard Java naming conventions, variable names start with lowercase characters.

于 2013-11-14T01:00:18.627 回答