我想点击一个按钮来获取EditText值。
String ETValue = ((EditText)findViewById(R.id.ETID)).getText().toString().trim();
在其他Android版本上,每个东西都能正常工作,但在1.6上我得到了“”(空)字符串。
Android 1.6上出了什么问题,这是怎么回事?
由于
屏幕截图:
代码参考:
main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_marginTop="10dip"
android:layout_marginLeft="5dip"
android:text="Type you text here"
android:id="@+id/TextView02"
android:layout_height="wrap_content"
android:textColor="#333333"
android:layout_width="fill_parent"
android:textSize="16dip">
</TextView>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/ID1">
<EditText android:layout_marginLeft="5dip"
android:id="@+id/keyword"
android:hint="e.g. Text here"
android:textSize="17dip"
android:singleLine="true"
android:layout_height="wrap_content"
android:layout_width="250dip"/>
<Button android:id="@+id/btnID"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:background="@drawable/icon"
android:gravity="bottom"
android:paddingBottom="9dip"
android:layout_marginLeft="3dip"/>
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/ID2"
android:visibility="gone">
<EditText android:layout_marginLeft="5dip"
android:id="@+id/keyword"
android:hint="e.g. Text here"
android:textSize="17dip"
android:singleLine="true"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_marginRight="5dip" />
</LinearLayout>
<Button android:text="Click" android:id="@+id/Button01" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
</LinearLayout>
Etext.Java
public class EText extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
((LinearLayout)findViewById(R.id.ID1)).setVisibility(View.GONE);
((LinearLayout)findViewById(R.id.ID2)).setVisibility(View.VISIBLE);
Button but = (Button) findViewById(R.id.Button01);
but.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
String ETValue = ((EditText) findViewById(R.id.keyword)).getText().toString().trim();
Toast.makeText(EText.this, ETValue, Toast.LENGTH_SHORT).show();
}
});
} }
即使这样也行不通
public class EText extends Activity {
/** Called when the activity is first created. */
EditText ETextt1 = null;
EditText ETextt2 = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if(Integer.parseInt(Build.VERSION.SDK) < 7){
((LinearLayout)findViewById(R.id.ID1)).setVisibility(View.GONE);
((LinearLayout)findViewById(R.id.ID2)).setVisibility(View.VISIBLE);
ETextt1 = ((EditText) findViewById(R.id.keyword1));
}else{
ETextt2 = ((EditText) findViewById(R.id.keyword2));
}
Button but = (Button) findViewById(R.id.Button01);
but.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
String ETValue = null;
if(null == ETextt1){
ETValue = ETextt2.getText().toString().trim();
}else if(null == ETextt2){
ETValue = ETextt1.getText().toString().trim();
}
Toast.makeText(EText.this, ETValue, Toast.LENGTH_SHORT).show();
}
});
} }
这完美无瑕: - )
package com.test.et;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
public class EText extends Activity {
/** Called when the activity is first created. */
EditText ETextt1 = null;
EditText ETextt2 = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if(Integer.parseInt(Build.VERSION.SDK) < 7){
((LinearLayout)findViewById(R.id.ID1)).setVisibility(View.GONE);
((LinearLayout)findViewById(R.id.ID2)).setVisibility(View.VISIBLE);
}
//ETextt = ((EditText) findViewById(R.id.keyword1));
Button but = (Button) findViewById(R.id.Button01);
but.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
String ETValue = null;
if(Integer.parseInt(Build.VERSION.SDK) < 7){
ETValue = ((EditText) findViewById(R.id.keyword2)).getText().toString().trim();
}else{
ETValue = ((EditText) findViewById(R.id.keyword1)).getText().toString().trim();
}
Toast.makeText(EText.this, ETValue, Toast.LENGTH_SHORT).show();
}
});
} }
答案 0 :(得分:7)
我发现了问题来源。您声明了两个具有相同ID的EditTexts。要解决问题,只需将EditText重命名为keyword1和keyword2。然后,从第二个EditText获取文本。
public class EdText extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
((LinearLayout)findViewById(R.id.ID1)).setVisibility(View.GONE);
((LinearLayout)findViewById(R.id.ID2)).setVisibility(View.VISIBLE);
Button but = (Button) findViewById(R.id.Button01);
but.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
String ETValue = ((EditText) findViewById(R.id.keyword2)).getText().toString().trim();
Toast.makeText(EdText.this, ETValue, Toast.LENGTH_SHORT).show();
}
});
} }
布局:
`
<TextView
android:layout_marginTop="10dip"
android:layout_marginLeft="5dip"
android:text="Type you text here"
android:id="@+id/TextView02"
android:layout_height="wrap_content"
android:textColor="#333333"
android:layout_width="fill_parent"
android:textSize="16dip">
</TextView>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/ID1">
<EditText android:layout_marginLeft="5dip"
android:id="@+id/keyword1"
android:hint="e.g. Text here"
android:textSize="17dip"
android:singleLine="true"
android:layout_height="wrap_content"
android:layout_width="250dip"/>
<Button android:id="@+id/btnID"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:background="@drawable/icon"
android:gravity="bottom"
android:paddingBottom="9dip"
android:layout_marginLeft="3dip"/>
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/ID2"
android:visibility="gone">
<EditText android:layout_marginLeft="5dip"
android:id="@+id/keyword2"
android:hint="e.g. Text here"
android:textSize="17dip"
android:singleLine="true"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_marginRight="5dip" />
</LinearLayout>
`
答案 1 :(得分:1)
我刚刚使用EditText挣扎,返回一个空字符串(“”)。
原因是setContentView(R.layout.main);
被调用了两次:
private EditText editText1;
private EditText editText2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); /// !!!
editText1=(EditText)findViewById(R.id.editText1);
editText2=(EditText)findViewById(R.id.editText2);
// some really long and untrivial initialization stuff
setContentView(R.layout.main); /// !!!
}
看起来editText1和editText2指向 R.layout.main
的前一个版本的部分......
答案 2 :(得分:0)
以下是我的案例测试示例:
public class EdText extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// string initialised here will always have initial value and never changes
final String ETValue = ((EditText) findViewById(R.id.EditText01)).getText().toString().trim();
Button but = (Button) findViewById(R.id.Button01);
but.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(EdText.this, ((EditText) findViewById(R.id.EditText01)).getText().toString().trim(), Toast.LENGTH_SHORT).show();
}
});
} }
API 4的一切正常。也许你的问题与读取字符串值时的情况有关。看我的评论。
答案 3 :(得分:0)
public class EditTextDemo extends Activity { /** 在第一次创建活动时调用。 * /
Button btnClick;
String ETValue;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ETValue = ((EditText)findViewById(R.id.editText1)).getText().toString().trim();
btnClick.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View view)
{
//assign a default value
ETValue.equals("Welcome!!");
Toast.makeText(getBaseContext(), "Value is:"+ETValue, Toast.LENGTH_SHORT).show();
Log.i("EditTextDemo","-----Value of EditText is:----------"+ETValue);
}
});
}}
您还可以在运行时给出值。此值也可用于进一步编码。 希望这会有所帮助!!