我在活动中开发了一个简单的表格 我的要求是,当我单击一个发送按钮时,表单数据将以相同的表单布局显示
但是该程序没有显示任何现场数据 没有错误。
Main_Activity.java:
public class MainActivity extends ActionBarActivity {
// private static final String EXTRA_MESSAGE = "com.example.";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendMessage(View view){
EditText editText1 =(EditText) findViewById(R.id.name);
String message1 =editText1.getText().toString();
Log.v("ThisApp", message1);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message1);
}
}
activity_main.xml中: -
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dip">
<EditText
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:text="@string/name"
android:background="#f3f3f3"
android:layout_marginTop="30dp"
android:hint="Name"/>
<Button
android:id="@+id/sendButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send"
android:onClick="sendMessage"
android:layout_gravity="center_horizontal" />
</LinearLayout>
我做错了吗?
答案 0 :(得分:0)
将TextView
放在相同的布局中:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<EditText
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:text="@string/name"
android:background="#f3f3f3"
android:layout_marginTop="30dp"
android:hint="Name"/>
<Button
android:id="@+id/sendButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send"
android:onClick="sendMessage"
android:layout_gravity="center_horizontal" />
<TextView
android:id="@+id/showText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="..."
android:background="#f3f3f3"
android:layout_marginTop="30dp"
android:hint="..."/>
</LinearLayout>
然后从MainActivity
:
public class MainActivity extends ActionBarActivity {
// private static final String EXTRA_MESSAGE = "com.example.";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button myButton=(Button)findViewById(R.id.sendButton);
EditText editText1 =(EditText) findViewById(R.id.name);
TextView textView = (TextView) findViewById(R.id.showText);
myButton.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
String message1 =editText1.getText().toString();
textView.setText(message1);
}
});
}
}