当前位置: 动力学知识库 > 问答 > 编程问答 >

android - i want change tablelayout tablerow backgroundcolor when user click tablerow

问题描述:

MainActivity.java

package com.example.abc.button_click_resize;

import android.graphics.Color;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.TableRow;

public class MainActivity extends AppCompatActivity {

TableRow tr = (TableRow)findViewById(R.id.first_row);

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

tr.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View arg0) {

tr.setBackgroundColor(Color.GREEN);

}

});

}

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<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"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

tools:context="com.example.abc.button_click_resize.MainActivity">

<TableLayout

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:stretchColumns="2">

<TableRow

android:id="@+id/first_row">

</TableRow>

</TableLayout>

</RelativeLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.example.abc.button_click_resize">

<application

android:allowBackup="true"

android:icon="@mipmap/ic_launcher"

android:label="button_click_resize"

android:supportsRtl="true"

android:theme="@style/AppTheme">

<activity android:name=".MainActivity">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

</application>

</manifest>

i googled and try to find in stackoverflow but couldn't find out where my code is wrong.

when i compile and run. it crashes.

could you tell me where is wrong?

where should i edit?

please. give me example.

thanks for reading.

分享给朋友:
您可能感兴趣的文章:
随机阅读: