2014-02-23 33 views
0

我正在編寫一個方法,它將讀取一個字符串,拆分一個字符串,然後在文本字段中顯示其中一個值。這是我到目前爲止,它正在編譯,但是當單擊該按鈕時,沒有任何內容填充到文本字段中。由於拆分字符串,設置令牌和文本視圖

import java.io.DataInputStream; 
import java.io.InputStream; 

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.*; 
import android.view.*; 

public class AboutScreen extends Activity{ 
    Button homeButton=null; 
    Button getReactant1=null; 
    TextView tv=null; 
    String is=null; 


@Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.about); 

    homeButton=(Button)findViewById(R.id.Home); 
    homeButton.setOnClickListener(new View.OnClickListener() { 
public void onClick(View v) { 
    finish(); 

    getReactant1=(Button)findViewById(R.id.combinationinterface); 
    getReactant1.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View v) { 
     String string1 = getResources().getString(R.string.MgandO); 
     String delims =","; 
     String[] tokens = string1.split(delims); 
     String i = null; 
     tokens[0]= i; 
     String f = null; 
     tokens[1]= f; 

     tv=(TextView)findViewById(R.id.R1TextInput); 
     is=i; 
     tv.setText(i); 
    } 
    }); 
    } 
    });} 

回答

0

因爲你所謂的onClick()開始的finish()方法。沒有做以下事情就停止了你的活動。

0

您的第一個問題是在homeButtonOnClickListener內設置按鈕的OnClickListener按鈕。

你的其他問題是使用不相關的變量,而不是在你的textView中使用拆分值。

嘗試使用和修改下面的代碼:

@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.about); 

     homeButton=(Button)findViewById(R.id.Home); 
     homeButton.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       finish(); 
      } 
     }); 


     getReactant1=(Button)findViewById(R.id.combinationinterface); 
     getReactant1.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       String string1 = getResources().getString(R.string.MgandO); 
       String delims =","; 
       String[] tokens = string1.split(delims); 
       String is=token[0]; 
       // or set it to another token you want i.e. token[1] 

       tv=(TextView)findViewById(R.id.R1TextInput); 
       tv.setText(is); 
      }  
     }); 
    }