我正在尝试在我的应用中使用微调器和按钮。我想知道我实现多个接口的语法是否正确。这就是目前的设置方式:
public class MainActivity extends AppCompatActivity
implements View.OnClickListener, AdapterView.OnItemSelectedListener
onItemSelectedListener的@Override也未被接受。我会发布我所拥有的内容但是我将省略处理按钮单击后发生的事情的部分,因为我没有错误,我认为它与此问题无关:
public class MainActivity extends AppCompatActivity
implements View.OnClickListener, AdapterView.OnItemSelectedListener {
Button setCountdown;
Spinner higestNotification;
EditText countdownTime;
int countdownConversion; //convert contents of countdownTime to an integer
int clockStart = 1;
int[] notificationTimes = new int[7];
String convertNotificationTimeToString;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setCountdown = (Button) findViewById(R.id.setCountdown);
higestNotification = (Spinner) findViewById(R.id.highestNotification);
countdownTime = (EditText) findViewById(R.id.countdownTime);
setCountdown.setEnabled(false);//disable button at the start of the app
higestNotification.setEnabled(false);// disable spinner at the start of the app
countdownTime.setOnClickListener(this);
setCountdown.setOnClickListener(this);
higestNotification.setOnItemSelectedListener(this);
}
@Override
public void OnItemSelected(AdapterView<?> arg0, View arg1, int position, long id )
{
}
@Override
public void OnNothingSelected(AdapterView<?> arg0)
{
}
正如我所说,错误是当我尝试实现View.OnClickListener和AdapterView.OnItemSelectedListener,以及OnItemSelected的覆盖
答案 0 :(得分:1)
您的重写方法应以小写字母开头。
正如您在代码中指定的那样,方法签名与接口合同不匹配。
尝试改为:
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("TEST", "onCreate");
}
@Override
public void onClick(View view) {
// handle onClick
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
// handle onItemSelected
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
// handle onNothingSelected
}
}
另外,Android Studio提供了代表您覆盖接口方法的快捷方式:Control + O
答案 1 :(得分:0)
您的方法SignIn
的大写字母OnItemSelected
与O
不同。
我在我的android-studio中试过这个并且它有效。
onItemSelected