-1

每当创建活动并在Android中向用户显示UI时,如何在按下按钮时显示按钮?

4

2 回答 2

1

这应该没问题:

@Override
public void onStart(){
    super.onStart();
    Button button = (Button) findViewById(R.id.test_button);
    button.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            Log.i(TAG, "Clicked");
        }
    });
    // This will NOT trigger the onClickListener!
    button.setPressed(true);
}
于 2012-10-11T16:08:50.947 回答
0

您应该使用按钮选择器状态,如以下代码 ie(res/drawable/button.xml)

<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_pressed="true"  android:drawable="@drawable/button_add_to_favorites_active">

</item>

<item android:state_focused="true" android:drawable="@drawable/button_add_to_favorites_active">

</item>

<item android:drawable="@drawable/button_add_to_favorites_active">  //Here set the drawable looks like pressed one.      

</item>
</selector>
于 2012-10-11T13:51:04.097 回答