0
   package walmart.namespace;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

导入 android.widget.EditText;导入 android.widget.TextView;

public class WalmartActivity extends Activity {
        /** Called when the activity is first created. */

        EditText name;
        Button search;
    TextView display;

        @Override
        public void onCreate(Bundle savedInstanceState) {
             setContentView(R.layout.main);   
            super.onCreate(savedInstanceState);

                name = (EditText)findViewById(R.id.etName);

                search = (Button) findViewById(R.id.btnSearch);

                display = (TextView) findViewById(R.id.tvdisplay);



                name.setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                                name.setText("");
                        }
                });
                search.setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                               if (name.equals("Electronics")) {
                                display.setText ('5');
                           } else if (name.equals("candy")) {
                                   display.setText ('1');
                           } else if (name.equals("Tobacco")) {
                               display.setText ("1");
                           } 
        }});
};}

无论我做什么,我的输出中都没有显示任何内容。我对 JAVA 非常非常陌生,所以我似乎无法找出为什么我的显示器上没有显示任何内容。

编辑将我的代码编辑为我目前拥有的。还是行不通。

4

1 回答 1

1

查看该代码,问题可能与您如何初始化名称和搜索有关。

更改您的代码:

name = (EditText) findViewById(getResources().getIdentifier("etName", "id", getPackageName()));
search = (Button) findViewById(getResources().getIdentifier("btnSearch", "id", getPackageName()));

对此:

name = (EditText) findViewById(R.id.etName);
search = (Button) findViewById(R.id.btnSearch);

此外,添加一个 TextView 以将您的答案显示到您的main.xml文件中,给它一个 id(例如显示)android:id="@+id/display",然后将其添加到您的活动中,以便您可以更改它:

display = (TextView) findViewById(R.id.display);

然后你可以通过调用来更新它:

display.setText('some text');

因此,您的文件的开头应如下所示:

public class WalmartActivity extends Activity {
    /** Called when the activity is first created. */

    EditText name;
    Button search;
    TextView display;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main); 

        name = (EditText) findViewById(R.id.etName);
        search = (Button) findViewById(R.id.btnSearch);
        display = (TextView) findViewById(R.id.display);

        ....

接下来更新您的if陈述:

if (name.equals("Electronics"))

到:

if (name.getText().toString().equals("Electronics")) {
    display.setText("something");
}

一段代码示例:

Demo2Activity.java

package com.demo1;

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

public class Demo2Activity extends Activity {
    private Button button;
    private EditText editText;
    private TextView textView;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);
        button = (Button) findViewById(R.id.button);
        editText = (EditText) findViewById(R.id.editText);
        textView = (TextView) findViewById(R.id.textText);

        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (editText.getText().toString().equals("Electronics")) {
                    textView.setText("found");
                }

            }
        });

    }
}

main2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="main2.xml"
        android:id="@+id/textText" />
    <EditText 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editText"/>
    <Button
        android:id="@+id/button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="button" />

</LinearLayout>
于 2012-05-01T00:08:03.927 回答