有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在Android的同一文本视图中添加不同的值

A希望使用SOAP服务协议在安卓中开发一个项目。所以我得到XML并将其解析为字符串数组。我想在一个文本视图中显示一个字符串(如字符串[0]),然后当用户单击“下一步”按钮时,下一个字符串(如字符串[1])将显示在同一页面和同一文本视图中。我怎么能这么做


共 (2) 个答案

  1. # 1 楼答案

    我不知道SOAP架构。但我假设您将解析的字符串存储为以下数组。我们还必须跟踪当前显示的字符串:

    static String[] myparsedarray;
    static int curIndex;
    

    将OnClick Listener添加到“下一步”按钮:

    class MyActivity extends Activity {
        //your string array
        static String[] myparsedarray;
        static int curIndex;
           .
           .
           .   //lots of other code
    
           // inside onCreate()
           public void onCreate(...) {
               Button nextBut = (Button) findViewById(R.id.nextbutton);
               nextBut.setOnClickListener(new OnClickListener() {
    
                   public void onClick(View v) {
                       TextView myloopingtext = (TextView) findViewById(R.id.mytextview);
                       curIndex = (curIndex + 1) % myparsedarray.length;
                       //the modulus to avoid arrayIndexOutOfBoundsException :)
                       myloopingtext.setText(myparsedarray[curIndex]);
                   }
               });
              .
              .
              .   //rest of the code
           }
    }
    

    其他人:如果我错了,请纠正我:)

    编辑:正如“不可分割”所说,请查看android教程。我还建议你搜索类似的问题。干杯

  2. # 2 楼答案

    也许你对安卓不太了解

    在要显示数据的主活动类中,创建字段字符串数组

    public class NNN extends Activity {
    //... some other fields
    
    private String[] data;
    private int current;
    Private TextView yourTextView;
    
    //in on create
    data = null;
    current = 0;
    yourTextView = (TextView) findViewById(R.id.myTextView);
    

    然后在解析字符串数组的XML的方法中

    data = parsedArray;
    current =0;
    

    然后在按钮上单击要将文本设置为文本视图的位置

    if(data ! = null) {
        //means you have parsed XML to string array
        if(current != data.length) {
            //means there is some data that user did not viewd
            youTextView.setText(data[current]);
            current++;
        }
    }
    

    你必须看一些教程才能熟悉android。见Creating hello world app