Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将textView从.xml获取到Java文件_Java_Android_Xml_Textview - Fatal编程技术网

如何将textView从.xml获取到Java文件

如何将textView从.xml获取到Java文件,java,android,xml,textview,Java,Android,Xml,Textview,我可以检查他们是否被点击 private TextView colorBoard[][]; this is my .java array. 但我只能指定当前单击的单元格。在我的游戏中,单元格的颜色会发生不同的变化。不是你刚才点击的那个。假设您单击1x3和4x4可能会更改颜色 我是android新手,这是我的第一个项目,如果我缺少一些基本的东西,非常抱歉。如果你想给TextView数组赋值。 然后在setContentView之后的oncreate方法中这样做 public void cellC

我可以检查他们是否被点击

private TextView colorBoard[][];
this is my .java array.
但我只能指定当前单击的单元格。在我的游戏中,单元格的颜色会发生不同的变化。不是你刚才点击的那个。假设您单击1x3和4x4可能会更改颜色


我是android新手,这是我的第一个项目,如果我缺少一些基本的东西,非常抱歉。

如果你想给TextView数组赋值。 然后在setContentView之后的oncreate方法中这样做

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
可以使用findviewbyId将TextView从xml转换为Java

假设你想在点击手机11时访问手机44,那么你可以这样做

  colorBoard[0][0] = (TextView) findViewById(R.id.cell11); 
 colorBoard[0][1] = (TextView) findViewById(R.id.cell12); 

访问cell44后,您可以设置背景色或任何其他属性

我认为您应该使用ListView而不是TextView。在ListView中,您可以使用数组将这些内容分配到您想要使用它们的任何位置

您可以创建如下布局文件:

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

android:weight=“匹配父对象”
android:height=“匹配父对象”
android:id=“+id/lst\u视图”
您的Java代码如下所示

<ListView>
  android:weight="match_parent"
  android:height="match_parent"
  android:id="+id/lst_view"
</ListView>
String[]array={“stack”、“queue”、“arraylist”};
ListView lst;
//在onCreate函数中
lst=(ListView)findViewById(R.id.lst_视图);
ArrayAdapter adpt=新的ArrayAdapter(这个,R.layout.what\u ever\u您的xml,array);
1.设置适配器(adpt);
//listview的功能
//arrayadapter用于将字符串类型数组转换为listview
lst.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
公共无效onItemClick(AdapterView AdapterView、View视图、int i、long l){
字符串s;
s=((TextView)视图).getText().toString();
Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG.show();
}
});

但我不是这个意思。它是随机分配的,所以我必须控制所有的。我的意思是,第一次点击11号单元格可能意味着44号单元格,但另一次也可能意味着23号单元格。您需要填充色板吗[]?不一定。但我必须能够访问任何我想要的细胞,并改变它的颜色。不用敲它。我的意思是我可以用ViewID填充,但这不是代码重复。还有更好的办法吗?我并不坚持在XML上创建文本视图(单元格),但我不太确定如何使它们程序化。请检查我更新的答案。您可以随时更改颜色,甚至无需点击,因为您可以访问每个单独的文本视图。我检查了您的答案,并在检查之前回答了我的答案。
<ListView>
  android:weight="match_parent"
  android:height="match_parent"
  android:id="+id/lst_view"
</ListView>
 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();
                }
            });