0

Android 開発と Java は初めてです。EditText の値を変数に格納し、後でコンソールに出力する際に​​問題があります。getText() について読んだことがありますが、それがどのように機能するのかよくわかりません。これが私のスクリプトです:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/simplenote_mainpage_background_200"
android:paddingBottom="0px"
android:paddingLeft="0px"
android:paddingRight="0px"
android:paddingTop="0px"
tools:context=".MainActivity" >

<RelativeLayout
    android:id="@+id/relativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</RelativeLayout>

<Button
    android:id="@+id/button1"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="41dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_marginBottom="2dp"
    android:layout_marginRight="1dp"
    android:text="Save"
    android:textColor="#FFFFFF"
    android:textSize="20sp"
    android:typeface="sans" />

<EditText
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/relativeLayout1"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="10dp"
    android:textStyle="bold"
    android:hint="Title.."
    android:ems="10" />

<EditText
    android:id="@+id/note"
    android:layout_width="285dp"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/title"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginTop="15dp"
    android:hint="Note.."
    android:ems="10" >

    <requestFocus />
</EditText>



</RelativeLayout>

.

    public class notescreen extends Activity {

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

    EditText text = (EditText)findViewById(R.id.title);
    String value = text.getText().toString();



   // TODO Auto-generated method stub
}

}

多くの問題が発生しているため、あなたが提案したことをいくつか試しました。スクリーンショットをアップロードしたいのですが、評判が10ありません。編集したコードは次のとおりです。

package com.example.simplenote;

import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;

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

    Button b1 = (Button)findViewById(R.id.button1);
    TextView tv = (TextView)findViewById(R.id.tv);
    EditText title = (EditText)findViewById(R.id.title);

     b1.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {

             String value = title.getText().toString();

            tv =settext(value);

                }
            });
     final String TAG = "MyClass";
             Log.d(TAG, value);
    //EditText text = (EditText)findViewById(R.id.title);
    //String value = text.getText().toString();



   // TODO Auto-generated method stub
}


  }
4

3 に答える 3

0
public class notescreen extends Activity {


//Its a good practice to declare all the things that you want in activity
String value;
Button b1;
TextView tv;
EditText title;

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

// here you have to initialize by its id
b1 = (Button)findViewById(R.id.button1);
tv = (TextView)findViewById(R.id.tv);
title = (EditText)findViewById(R.id.title);

 b1.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

         value = title.getText().toString();

        tv.settext(value);

            }
        });


  }

}

xml ファイルで TextView を作成します

<TextView
android:id="@+id/tv"
android:layout_width="285dp"
android:layout_height="wrap_content"

 >
于 2013-10-04T12:45:49.037 に答える
0

これを試して:

//Add your packagename here please

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class notescreen extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        // Here is where you call the super on this method.
        // Mostly always comes as first line of any onCreate.
        super.onCreate(savedInstanceState);

        // After you have your layout set, you can retrieve your references
        setContentView(R.layout.notescreen);

        // You have just recovered your view class
        final EditText text = (EditText) findViewById(R.id.title);

        // You have just recovered the text of that view class (text)
        // final String value = text.getText().toString();

        // The log works as a printer on the console
        // Log.d("notescreen", "This is the text: " + value);

        // But hey... there is no text right "onCreate", right?
        // Soo...

        // Retrieve a button (you will need to create it on XML first)
        final Button clickForTextBt = (Button) findViewById(R.id.clickfortextbt);

        // Make that button print your message
        clickForTextBt.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Log.d("notescreen", "This is the text: " + text.getText());
            }
        });
    }
}
于 2013-10-04T13:30:30.797 に答える