![](/img/trans.png)
[英]How to change text color when Button/TextView is pressed in android
[英]Button pressed and change textview color
谁能看到我的代码有什么问题:(我什至无法创建一个简单的应用程序,它不断崩溃。
该应用程序要做的是当用户按下按钮时,文本视图将颜色更改为红色。
public class MainActivity extends Activity {
public TextView tv1;
public Button bt1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt1 = (Button)findViewById(R.id.button1);
tv1 = (Button)findViewById(R.id.textView1);
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
tv1.setTextColor(Color.BLACK);
}
});
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Button" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button1"
android:layout_toRightOf="@+id/button1"
android:text="TextView" />
tv1 = (Button)findViewById(R.id.textView1);
用。。。来代替
tv1 = (TextView)findViewById(R.id.textView1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.