如果选择该按钮以更改文字大小

时间:2016-05-05 16:14:22

标签: java android android-tv

这一点必须公平,当选择按钮来改变文字大小时? 我尝试添加一个onCreate但是不能正常工作,只是一次启动应用程序。该功能下次不会保持活动状态。 谢谢

按钮

        <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/inbox_string"
        android:id="@+id/button"
        android:textColor="@drawable/text_button_culoare"
        android:drawableLeft="@drawable/ic_view_list_white_24dp"
        android:background="@android:color/transparent"
        style="?android:attr/borderlessButtonStyle"
        android:layout_centerHorizontal="true"
        android:gravity="left|center_vertical"
        android:layout_gravity="center_horizontal"
        android:focusable="true"
        android:enabled="true"
        android:clickable="true"
        android:contextClickable="true"
        android:elegantTextHeight="true"
        android:layout_marginTop="16dp" />

的活动:

public class Work_screen extends AppCompatActivity {

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


        View.OnClickListener clickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v.getId() == R.id.button) {


                }
            }
        };

        findViewById(R.id.button).setOnClickListener(clickListener);



        final Button button = (Button) findViewById(R.id.button);

        if (button.isSelected()) {
            Context context = getApplicationContext();
            CharSequence text = "selected";
            int duration = Toast.LENGTH_SHORT;

            Toast toast = Toast.makeText(context, text, duration);
            toast.show();
            button.setTextSize(22);
        } else {
            Context context = getApplicationContext();
            CharSequence text = "not";
            int duration = Toast.LENGTH_SHORT;

            Toast toast = Toast.makeText(context, text, duration);
            toast.show();
        }

    }
}

1 个答案:

答案 0 :(得分:1)

您应该在onClick方法中放置一些代码,因为单击该按钮时会调用此方法。第一次调用代码的原因是ONCREATE方法只是在创建应用程序时调用一次。

只需将if else语句放在onClick方法中即可。 你应该创建你的按钮变量作为类变量,以便在你的监听器中访问它,即使它不是你的onCreate方法的一部分。