有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java我对一个显示器的问题有一个非常不切实际的询问

   package walmart.namespace;

import 安卓.app.Activity;
import 安卓.os.Bundle;
import 安卓.view.View;
import 安卓.widget.Button;

导入安卓。小装置。编辑文本; 导入安卓。小装置。文本视图

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非常、非常陌生,所以我似乎不知道为什么我的显示器上什么都没有显示

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


共 (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);
    

    另外,添加一个文本视图,将您的答案显示到main.xml文件中,给它一个id(比如说display)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");
    }
    

    下面是一段代码示例:

    人口活动。爪哇

    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");
                    }
    
                }
            });
    
        }
    }
    

    缅因州2。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>