【发布时间】:2017-08-05 12:20:32
【问题描述】:
我尝试在SwitchCompat 上设置文本,但它不起作用。它只在第一次工作。但是当您尝试更改文本时(例如单击按钮时),它不起作用。
例如:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final SwitchCompat switchCompat = (SwitchCompat)findViewById(R.id.switch_test);
switchCompat.setTextOn("Yes");
switchCompat.setTextOff("No");
switchCompat.setShowText(true);
Button buttonTest = (Button)findViewById(R.id.button_test);
buttonTest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switchCompat.setTextOn("YOO");
switchCompat.setTextOff("NAH");
//switchCompat.requestLayout(); //tried to this but has no effect
//switchCompat.invalidate(); //tried to this but has no effect
}
});
}
您会看到文本保持为 Yes 和 No。我试过打电话给requestLayout() 和invalidate(),但没有成功。有什么想法吗?
【问题讨论】:
标签: java android android-layout android-view switchcompat