我正在从斯坦福大学在线学习Java来制作一个简单的android项目。 它需要制作一个简单的购物清单。
要求:当用户点击时当用户点击“添加项目”时,EditText中当前的任何内容都应添加到购物列表的底部,当用户点击"清除列表" ,你应该清除TextView的内容。
错误:当用户点击按钮时,我在向TextView添加项目时遇到了困难。
很难描述这个错误所以我把我项目的图片放在这里:
该项应该如下图所示:
如果有人能说出我的错误是什么以及我应该做什么,那就太好了,谢谢
以下是我的代码:
package com.stanford.ca108.shoppinglist;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void add(View view){
TextView textView = (TextView)findViewById(R.id.textoutput);
EditText edit = (EditText)findViewById(R.id.input);
String a = edit.getText().toString();
textView.append(a);
}
public void cleantext(View view){
TextView textView = (TextView)findViewById(R.id.textoutput);
textView.setText(" ");
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.stanford.ca108.shoppinglist.MainActivity">
<TextView
android:id="@+id/textoutput"
android:layout_width="match_parent"
android:layout_height="350dp"
android:textSize="20sp"
android:text="Please List your items" />
<EditText
android:id="@+id/input"
android:layout_above="@+id/bgroup"
android:hint=" "
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:id="@+id/bgroup"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:text = "additem"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="add"
/>
<Button
android:text = "clear list"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="cleantext"
/>
</LinearLayout>
</RelativeLayout>
答案 0 :(得分:1)
//XML layout in the propertise of the Additem button
android:onClick="additem"
// in Java Class
//set an onclick event listener for the Additem Button
public void additem(View view) {
String itme1 = editview1.getText.toString;
String itme2 = editview2.getText.toString;
String itme3 = editview3.getText.toString;
String TotalItem = item1+"\n"+item2+"\n"+item3;
ShoplistTextView.setText(TotalItem);
// this would trigger the method that would set all edittext to be //empty is the cart has been filled after
//pressing the additem button
checkifShoplistIsNotEmpty();
}
public void checkifShoplistIsNotEmpty(){
String check =cartTextView.getText.toString;
if (!check.isEmpty()){
editview1.setText("");
editview2.setText("");
editview3.setText("");
}