如何从EditText打开新活动

时间:2016-10-19 10:33:06

标签: android

我试过这个不工作为什么。我应该在我的代码中改变什么。就像用户输入打开新活动一样。他想要什么活动!!

public class MainActivity extends AppCompatActivity {
    @Override
    public Editable location;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        EditText input = (EditText) findViewById(R.id.input);
        location=input.getText();
    }


    public void buttonClicked(View v) {

        switch (v.getId()) {
            case R.id.benter: if (location.equals("khilgaon")) {
                Intent intent = new Intent(MainActivity.this, khilgaon.class);
                startActivity(intent);
            } else if (location.equals("shantinagar")) {
                Intent intent = new Intent(MainActivity.this, shantinagar.class);
                startActivity(intent);
            }
                break;
        }
    }
    }

4 个答案:

答案 0 :(得分:0)

EditText input = (EditText) findViewById(R.id.input);

input.setOnKeyListener(new OnKeyListener()
    {
        public boolean onKey(View v, int keyCode, KeyEvent event)
        {
            if (event.getAction() == KeyEvent.ACTION_DOWN)
            {
                switch (keyCode)
                {
                    case KeyEvent.KEYCODE_DPAD_CENTER:
                    case KeyEvent.KEYCODE_ENTER:

                        switch(input.getText().ToString()) {

                            case "khilgaon" :
                               Intent p = new Intent(MainActivity.this, khilgaon.class);      
                               startActivity(p);
                               break;

                            case "shantinagar" :
                               Intent q = new Intent(MainActivity.this, shantinagar.class);
                               startActivity(q);
                               break;
                           default:
                               //msg: activity does not exist

                         }

                        return true;

                    default:
                        break;
                }
            }
            return false;
        }
    });

答案 1 :(得分:0)

你必须写

         location = input.getText();

                  public void buttonClicked(View v) 
                    {

                    }

这种方法

答案 2 :(得分:0)

你有没有试过这个?

private EditText input;
private Button btnGo;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    input = (EditText) findViewById(R.id.input);
    btnGo = (Button) findViewById(R.id.btnGo);

    btnGo.setOnClickListener(new View.OnClickListener() {
        public String str;

        @Override
        public void onClick(View view) {
            str = input.getText().toString().trim();
            Log.e("String ", "str" + str);
            if (str.equals("SecondActivity")) {
                Intent i = new Intent(MainActivity.this, SecondActivity.class);
                startActivity(i);
            }
        }
    });
}

答案 3 :(得分:0)

void buttonClicked(){
    location = input.getText();

    if(location.equalsIgnoreCase("khilgaon")){
        ///intent
    }else if (location.equalsIgnoreCase("shantinagar")){
        ///intent
    }
}