下面的代碼:ImageView不會改變setImageResource()?
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
ImageView button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (ImageView)findViewById(R.id.startButton);
button.setOnClickListener(this);
button.setImageResource(R.drawable.play);
}
public void onClick(View v) {
button.setImageResource(R.drawable.pause);
}
}
顯然,如果我把button.setImageResource(R.drawable.pause);
在OnCreate它的工作原理,並改變圖像,但不是在onclick?我試着調試它,它運行onClick代碼,但沒有更改按鈕圖像。我可能犯了一個非常愚蠢的錯誤,但我無法弄清楚。
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.larry.app.MainActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/startButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true" />
</LinearLayout>
向我們展示您的xml請 –
我不認爲它的xml –
我重做你的代碼在一個例子中,它工作正常(setImageResource作品)。你有沒有添加其他的東西? –