gpt4 book ai didi

安卓动态onClick

转载 作者:行者123 更新时间:2023-11-29 18:10:37 25 4
gpt4 key购买 nike

第一次发帖请温柔...

这段代码在 textviews simples 中创建了一个 10 x 10 的表格,里面全是连字符...

对于 onClickListner,我需要它在单击文本时更改颜色。

它在 tv.setTextColor(Color.RED); 上失败

可以排序还是有更好的办法??

谢谢。

_临时类:

public class _Temp extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a_temp);


TableLayout treeTable = (TableLayout) findViewById(R.id.main_table);

// Add 10 Rows / 10 Cols ~~~~~~~~~~~~~~~~~~~~~~~~~~
int idxCount = 0;

for (int tree = 1; tree <= 10; tree++) {
idxCount = 0;

TableRow TableRows = new TableRow(this);
TableRows.setId(tree * 100 + idxCount);
TableRows.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
TableRows.setBackgroundColor(Color.LTGRAY);
idxCount++;

for (int bird = 1; bird <= 10; bird++) {
TextView label_TableCols2 = new TextView(this);
label_TableCols2.setId(tree * 100 + idxCount);
label_TableCols2.setBackgroundColor(Color.DKGRAY);
label_TableCols2.setClickable(true);
label_TableCols2.setOnClickListener(this);

label_TableCols2.setText("-");

TableRows.addView(label_TableCols2); // add the column to the table row
idxCount++;
}
treeTable.addView(TableRows, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
}
}

public void onClick(View v) {
Log.i("scoreTable", "Something Clicked - ID = " + Integer.toString(v.getId()));

int resId = getResources().getIdentifier(Integer.toString(v.getId()), "id", getPackageName());

Log.i("scoreTable", "Colour Update 1 - resId = " + Integer.toString(resId));

TextView tv = (TextView) findViewById(resId);

Log.i("scoreTable", "Colour Update 2 - Just to see if it gets this far");

tv.setTextColor(Color.RED);

Log.i("scoreTable", "Colour Update 3 - Just to see if it gets this far");
}
}

和a_temp.xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content" >

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_table"
android:layout_width="match_parent"
android:layout_height="fill_parent" >

</TableLayout>

</LinearLayout>

</HorizontalScrollView>

</ScrollView>

编辑:好吧,这太简单了……用充气机怎么样!!

因此,单击表格充气器弹出窗口中的项目,选择颜色并单击“选择颜色”按钮,更新单击的连字符的颜色。

public void onClick(View v) {
((TextView) v).setTextColor(Color.RED); // <--- SWEET, WORKING

AlertDialog.Builder PopUpBuilder = new AlertDialog.Builder(this);

//...

PopUpBuilder.setPositiveButton("Choose Colour", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {

// Do stuff and store global variables

updateTable();
}
});
AlertDialog PopupDialog = PopUpBuilder.create();
PopupDialog.show();
}

public void updateTable() {

TextView scoreTableTitle = (TextView) findViewById(R.id.textTitle1);
scoreTableTitle.setText("woo woo"); // <--- WORKING

((TextView) v).setTextColor(Color.BLUE); // <--- ????
}

最佳答案

点击的 View 是您的 TextView,所以您只需在您的监听器上设置他的颜色即可:

public void onClick(View v) {
((TextView) v).setTextColor(Color.RED);
}

关于安卓动态onClick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10950192/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com