2016-11-05 98 views
0

我對Java有一些經驗,但我現在正在嘗試創建一個Android應用程序,我不知道如何接受用戶輸入。我的編輯文本的id是enterRequestMessage,我的XML文件的名稱是activity_score_entry.xml。這是我的Java文件的開始。接受來自Activity的用戶輸入

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 

public class ScoreEntry extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_score_entry); 

    } 
} 
+0

您何時想獲取用戶輸入。 onValueChange或按鈕點擊 – zombie

+0

哦,我忘了。我想要按鈕點擊此按鈕:輸入非常感謝您的幫助 – mike

+0

在按鈕上設置onClickListener()並檢查EditText的值。順便說一句,你知道findViewById()你不是 – zombie

回答

2

您需要在XML中

public class ScoreEntry extends AppCompatActivity implements View.OnClickListener { 

    private EditText editText; 
    private Button button; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_score_entry); 

     editText = (EditText) findViewById(R.id.editText); 
     button = (Button) findViewById(R.id.button); 

     button.setOnClickListener(this); 
    } 

    @Override 
    public void onClick(View view) { 
     if (view.getId() == R.id.button) { 
      String value = editText.getText().toString(); 

      // use the value here 
      Log.d(ScoreEntry.class.getCanonicalName(), "The value is: " + value); 
     } 
    } 
} 

並且XML

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/activity_score_entry" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <EditText 
     android:id="@+id/editText" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

    <Button 
     android:id="@+id/button" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

</LinearLayout> 

訪問控制,您可以根據自己的需要調整代碼。