2016-11-05 20 views
0

私はJavaでいくつかの経験がありますが、今はAndroidアプリを作成しようとしていますが、ユーザの入力を受け入れる方法がわかりません。私の編集テキストのIDはenterRequestMessageで、私のXMLファイルの名前はactivity_score_entry.xmlです。ここに私のJavaファイルの始まりがあります。アクティビティからユーザ入力を受け入れる

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 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> 
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のコントロールへのアクセスを取得する必要があります。

関連する問題