获取在EditText中输入的文本并将其显示在不同的活动

时间:2016-08-30 20:58:25

标签: android listview android-edittext

我的问题是如何在edittext中输入文本并以不同的布局显示。就像一个人输入文字并按下确定按钮一样。我想存储在edittext中输入的文本,然后在listview中显示它。我的MainActivity类是:

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.net.URI;
import java.net.URISyntaxException;

public class MainActivity extends Activity {

    Button search;
    Button website;
    EditText onlineEdit;
    TextView text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        onlineEdit = (EditText) findViewById(R.id.searchEdit);
        text = (TextView) findViewById(R.id.textView);

        search = (Button) findViewById(R.id.searchGoogle);
        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

if(onlineEdit.getText().toString().startsWith(" ")) {
                        Toast.makeText(getApplicationContext(), "No results!", Toast.LENGTH_SHORT).show();
                    } else {
                        try {
                        URI searchOnline = new URI("https://www.google.com/search?q=" + 

onlineEdit.getText().toString().replace(" ", "+"));
                        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(String.valueOf(searchOnline)));
                        startActivity(browserIntent);
                    } catch (URISyntaxException e) {
                       Toast.makeText(getApplicationContext(), "Invalid URL", Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            });
            website = (Button) findViewById(R.id.openWebsite);
            website.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                if(onlineEdit.getText().toString().startsWith(" ")) {
                    Toast.makeText(getApplicationContext(), "Please enter a valid URL", Toast.LENGTH_SHORT).show();
                } else if(onlineEdit.getText().toString().equals("")) {
                    Toast.makeText(getApplicationContext(), "Please enter a URL", Toast.LENGTH_SHORT).show();
                } else if (!(onlineEdit.getText().toString().contains("https://") && onlineEdit.getText().toString().contains(".com"))) {
                    onlineEdit.setText("https://" + onlineEdit.getText().toString() + ".com");
                } else if(!(onlineEdit.getText().toString().contains("https://"))) {
                    onlineEdit.setText("https://" + onlineEdit.getText().toString());
                } else if(!(onlineEdit.getText().toString().contains(".com"))) {
                    onlineEdit.setText(onlineEdit.getText().toString() + ".com");
                } else {
                try {
                    URI goOnline = new URI(onlineEdit.getText().toString());
                    Intent browserIntent2 = new Intent(Intent.ACTION_VIEW, Uri.parse(String.valueOf(goOnline)));
                    startActivity(browserIntent2);
                } catch (URISyntaxException e) {
                    Toast.makeText(getApplicationContext(), "Invalid URL", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });

        search.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                startActivity(new Intent(MainActivity.this, HistoryActivity.class));
                return true;
                }
        });
        website.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                startActivity(new Intent(MainActivity.this,                                                     HistoryActivity.class));
                return true;
            }
        });

    }
}

接下来是我的HistoryActivity:

package applications.codegeek.com.goonline;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class HistoryActivity extends Activity {

    ListView listView;
    EditText search;
    TextView text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);

        listView = (ListView) findViewById(R.id.listView);
        search = (EditText) findViewById(R.id.searchEdit);
        text = (TextView) findViewById(R.id.textView);

        String[] history = new String[]{
                text.getText().toString()
        };
        ArrayAdapter<String> adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1,
                android.R.id.text1, history);

        // assign list view to adapter
        listView.setAdapter(adapter);

        // set on click
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                int intPosition = position;
                String clickedValue = listView.getItemAtPosition(intPosition).toString();
                Toast.makeText(getApplicationContext(), clickedValue, Toast.LENGTH_LONG).show();

            }
        });
    }
}

请原谅我任何不正确的格式,因为我是初学者。

编辑:我跟着DanielOrdóñez的回答,现在我收到了以下错误 -

09-05 11:00:34.022 20307-20307/applications.codegeek.com.goonline E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                Process: applications.codegeek.com.goonline, PID: 20307
                                                                                java.lang.RuntimeException: Unable to start activity ComponentInfo{applications.codegeek.com.goonline/applications.codegeek.com.goonline.HistoryActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
                                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2358)
                                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420)
                                                                                    at android.app.ActivityThread.access$900(ActivityThread.java:154)
                                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                    at android.os.Looper.loop(Looper.java:135)
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:5292)
                                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                                    at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
                                                                                 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
                                                                                    at applications.codegeek.com.goonline.HistoryActivity.onCreate(HistoryActivity.java:34)
                                                                                    at android.app.Activity.performCreate(Activity.java:5990)
                                                                                    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
                                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420) 
                                                                                    at android.app.ActivityThread.access$900(ActivityThread.java:154) 
                                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) 
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                    at android.os.Looper.loop(Looper.java:135) 
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:5292) 
                                                                                    at java.lang.reflect.Method.invoke(Native Method) 
                                                                                    at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) 
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)

HistoryActivity中的第34行是bundle.getString("CONSTANT_NAME");

1 个答案:

答案 0 :(得分:1)

  1. EditText获取信息,其中包含:getText() https://developer.android.com/reference/android/widget/EditText.html#getText()
  2. 2.您可以通过两种方式发送数据。

    //Create the bundle
    Bundle bundle = new Bundle();
    
    //Add your data from getFactualResults method to bundle
    bundle.putString("CONSTANT_NAME", EditText.getText);
    
    //Add the bundle to the intent
    browserIntent.putExtras(bundle);
    
    startActivity(browserIntent);
    

    在您应该使用的其他活动中:

    Bundle bundle = getIntent().getExtras();
    
    //Extract the data…
    String value = bundle.getString("CONSTANT_NAME");        
    
    if (bundle.containsKey(MainActivity.CONSTANT_NAME))    {
        ....
    }