如何从 .xml 文件获取 TextViews 到 Java 文件
how to get TextViews from .xml to Java file
<TableRow
android:id="@+id/Row3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center">
<TextView
android:id="@+id/cell31"
android:layout_height="fill_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center"
android:textSize="50sp"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceLarge"
android:background="@drawable/back"
android:onClick="cellClick"
android:clickable="true"/>
<TextView
android:id="@+id/cell32"
android:layout_height="fill_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center"
android:textSize="50sp"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceLarge"
android:background="@drawable/back"
android:onClick="cellClick"
android:clickable="true"/>
.... this is 5x5 row column table. with 25 cells in it.
我的 .xml 文件如下所示。我在 LinearLayout 中有一堆行包含 TableLayouts,其中包含 TextViews 作为单元格。我正在和他们一起做一个基本的游戏。
我正在尝试将 .xml 中的每个单元格分配给 .java 文件中的 TextView 数组。但我无法弄清楚如何做到这一点。我需要根据我制作的某种算法更改每个单元格的颜色,所以我想访问单元格。
private TextView colorBoard[][];
this is my .java array.
我可以检查它们是否被点击
public void cellClick(View v) {
TextView cell = (TextView) findViewById(v.getId());
switch (cell.getId()) {
case R.id.cell11:
xloc = 0;
break;
case R.id.cell12:
xloc = 1;
break ;
...rest of cells
但我只能指定在 moment.In 处单击的单元格我的游戏单元格颜色会发生不同变化。不是您刚刚单击的那个。假设您单击 1x3 和 4x4 可能会改变颜色。
我是 android 的新手,这是我的第一个项目,如果我遗漏了一些基本的东西,非常抱歉。
如果你想给TextView数组赋值。
然后在 setContentView
之后的 oncreate 方法中这样做
colorBoard[0][0] = (TextView) findViewById(R.id.cell11);
colorBoard[0][1] = (TextView) findViewById(R.id.cell12);
您可以使用 findviewbyId 获取从 xml 到 Java 的 TextView。
假设你想在点击cell11时访问cell44,那么你可以这样做。
public void cellClick(View v) {
TextView cell = (TextView) findViewById(v.getId());
switch (cell.getId()) {
case R.id.cell11:
xloc = 0;
colorBoard[0][0].setBackground()//As you wish
break;
case R.id.cell12:
xloc = 1;
break ;
...rest of cells
一旦您有权访问 cell44,您就可以为其设置背景颜色或任何其他属性。
我认为您应该使用 ListView 而不是 TextView。在 ListView 中,您可以使用数组将这些东西分配到您想要使用它们的任何地方。
您可以这样创建布局文件:
<ListView>
android:weight="match_parent"
android:height="match_parent"
android:id="+id/lst_view"
</ListView>
您的 Java 代码如下所示
String[] array={"stack","queue","arraylist"};
ListView lst;
//In onCreate function
lst=(ListView)findViewById(R.id.lst_view);
ArrayAdapter<String> adpt=new ArrayAdapter<String>(this,R.layout.what_ever_your_xml,array);
lst.setAdapter(adpt);
//function of listview
//arrayadapter is used for converting string type array into listview
lst.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String s;
s=((TextView)view).getText().toString();
Toast.makeText(getApplicationContext(), s,Toast.LENGTH_LONG).show();
}
});
<TableRow
android:id="@+id/Row3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center">
<TextView
android:id="@+id/cell31"
android:layout_height="fill_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center"
android:textSize="50sp"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceLarge"
android:background="@drawable/back"
android:onClick="cellClick"
android:clickable="true"/>
<TextView
android:id="@+id/cell32"
android:layout_height="fill_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center"
android:textSize="50sp"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceLarge"
android:background="@drawable/back"
android:onClick="cellClick"
android:clickable="true"/>
.... this is 5x5 row column table. with 25 cells in it.
我的 .xml 文件如下所示。我在 LinearLayout 中有一堆行包含 TableLayouts,其中包含 TextViews 作为单元格。我正在和他们一起做一个基本的游戏。 我正在尝试将 .xml 中的每个单元格分配给 .java 文件中的 TextView 数组。但我无法弄清楚如何做到这一点。我需要根据我制作的某种算法更改每个单元格的颜色,所以我想访问单元格。
private TextView colorBoard[][];
this is my .java array.
我可以检查它们是否被点击
public void cellClick(View v) {
TextView cell = (TextView) findViewById(v.getId());
switch (cell.getId()) {
case R.id.cell11:
xloc = 0;
break;
case R.id.cell12:
xloc = 1;
break ;
...rest of cells
但我只能指定在 moment.In 处单击的单元格我的游戏单元格颜色会发生不同变化。不是您刚刚单击的那个。假设您单击 1x3 和 4x4 可能会改变颜色。
我是 android 的新手,这是我的第一个项目,如果我遗漏了一些基本的东西,非常抱歉。
如果你想给TextView数组赋值。 然后在 setContentView
之后的 oncreate 方法中这样做 colorBoard[0][0] = (TextView) findViewById(R.id.cell11);
colorBoard[0][1] = (TextView) findViewById(R.id.cell12);
您可以使用 findviewbyId 获取从 xml 到 Java 的 TextView。
假设你想在点击cell11时访问cell44,那么你可以这样做。
public void cellClick(View v) {
TextView cell = (TextView) findViewById(v.getId());
switch (cell.getId()) {
case R.id.cell11:
xloc = 0;
colorBoard[0][0].setBackground()//As you wish
break;
case R.id.cell12:
xloc = 1;
break ;
...rest of cells
一旦您有权访问 cell44,您就可以为其设置背景颜色或任何其他属性。
我认为您应该使用 ListView 而不是 TextView。在 ListView 中,您可以使用数组将这些东西分配到您想要使用它们的任何地方。
您可以这样创建布局文件:
<ListView>
android:weight="match_parent"
android:height="match_parent"
android:id="+id/lst_view"
</ListView>
您的 Java 代码如下所示
String[] array={"stack","queue","arraylist"};
ListView lst;
//In onCreate function
lst=(ListView)findViewById(R.id.lst_view);
ArrayAdapter<String> adpt=new ArrayAdapter<String>(this,R.layout.what_ever_your_xml,array);
lst.setAdapter(adpt);
//function of listview
//arrayadapter is used for converting string type array into listview
lst.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String s;
s=((TextView)view).getText().toString();
Toast.makeText(getApplicationContext(), s,Toast.LENGTH_LONG).show();
}
});