Suppose I have two buttons, one is an action button which is in the action bar(@+id/button1). And another is common button in the layout(@+id/button2).
How can I set the button1 disabled when i click the button2?
findViewById(button1) seems not work. it will return null.
this is my menu xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/button1"
android:title="submit"
android:showAsAction="always" />
</menu>
and this is my mainacticity:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.button1 ) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
button1 = (Button) findViewById(R.id.button1);/*which return null*/
button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
button1.setEnabled(false);/*what i failed to do*/
}
});
}