2012-02-20 67 views
0

我有一個呈現的表單,但是當我單擊提交按鈕時,我不確定如何獲得結果。無法從Android中的表單獲取用戶輸入

這裏是我的Activity類:

package com.problemio; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

public class AddProblemActivity extends Activity 
{ 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.add_problem); 


     final EditText problemName = (EditText) findViewById(R.id.problem_name); 
     String name = problemName.getText().toString(); 

     final EditText problemText = (EditText) findViewById(R.id.problem_text); 
     String text = problemText.getText().toString();   

    System.out.println(name);  
    System.out.println(text);   




//  Button browseProblemsButton = (Button)findViewById(R.id.browseProblemsButton); 
//  Button searchProblemsButton = (Button)findViewById(R.id.searchProblemsButton); 
//  Button myProblemsButton = (Button)findViewById(R.id.myProblemsButton);   


    } 

    public void sendFeedback(View button) 
    { 


     System.out.println("3"); 
     Button addProblemButton = (Button)findViewById(R.id.button_send_feedback); 

     addProblemButton.setOnClickListener(new Button.OnClickListener() 
     { 
      public void onClick(View v) 
      { 
       System.out.println("4"); 

       //Intent myIntent = new Intent(ProblemioActivity.this, AddProblem.class); 
       //ProblemioActivity.this.startActivity(myIntent); 
      } 
     });   
     System.out.println("end"); 

     // Do click handling here 
    }  
} 

我擺在那裏的System.out一些語句,看看是否有什麼被寫入到我的日誌輸出,但沒有奏效。我是Android開發新手 - 有沒有一種常用的方法來輸出東西到控制檯?

我也不確定在哪裏以及如何從表單元素獲得輸入。

幫助表示讚賞, 亞歷

回答

3

即使用戶輸入了以下內容。你實際上擁有了你需要的所有代碼,只是沒有正確放置。你的聽衆應該是這樣的,並放置在onCreate()方法(如果我理解正確你的意圖):

Button addProblemButton = (Button)findViewById(R.id.button_send_feedback); 
addProblemButton.setOnClickListener(new Button.OnClickListener() 
{ 
    public void onClick(View v) 
    { 
     String name = problemName.getText().toString(); 
     String text = problemText.getText().toString(); 
     sendFeedback(name, text); 
    } 
}); 

然後你sendFeedback功能看起來像:

private void sendFeedback(String name, String text){ 
    //Do what ever you need to here for sending feed back. 
} 

由於@onit提到,您應該使用Log類打印調試信息。具體爲Log.d()

+0

並且此偵聽器應位於sendFeedback()方法或onCreate()方法內?謝謝! – Genadinik 2012-02-20 22:17:21

+0

對不起,清除我的答案有點 – 2012-02-20 22:20:24

+0

謝謝,那太棒了!我剛剛接受了答案。如果我可以再問一個問:如果我需要在屏幕上顯示錯誤消息,或者將數據插入遠程數據庫,那該怎麼做? – Genadinik 2012-02-20 22:26:31

1

你的問題是,你正在使用的System.out。默認情況下,System.out輸出到/ dev/null,所以沒有輸出。您可以使用Android Debug Bridge重定向System.out,將數據打印到日誌中。您也可以使用Log課程將信息發佈到您的日誌。