我是 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
}
}