简体   繁体   中英

Button pressed and change textview color

Can anyone see what is wrong with my code :( I can't even create a simple app, it keep crashing it.

What the app is going to do is when user press the button the textview will change colour to RED.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM