如何在Android Studio中使用EditText实现页面跳转:

huangapple go评论75阅读模式
英文:

How to use EditText to go another activity in Android Studio

问题

在Android Studio的一个活动中,我想使用EditText输入来跳转到第二个活动,我的代码能够工作,但我必须按两次才能实现,如何更正为只需按一次

 editText.setInputType(InputType.TYPE_NULL);
 editText.setShowSoftInputOnFocus(false);

 public void addListenerOnButton() {
    editText.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            String text = editText.getText().toString();
            Intent myIntent = new Intent(view.getContext(), Calculated.class);
            myIntent.putExtra("mytext", text);
            startActivity(myIntent);
        }
    });
}
英文:

In an Android studio activity I want to use the EditText input to go to a second activity, my code works, but I have to press twice for this to work, how can I correct this to only press once

 editText.setInputType(InputType.TYPE_NULL);
 editText.setShowSoftInputOnFocus(false);

 public void addListenerOnButton() {
    editText.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            String text = editText.getText().toString();
            Intent myIntent = new Intent(view.getContext(),Calculated.class);
            myIntent.putExtra("mytext",text);
            startActivity(myIntent);

        }
    });
}

答案1

得分: 1

为什么要将EditText的监听器包装在另一个监听器中?<br/>
更改为以下方式:

editText.setInputType(InputType.TYPE_NULL);
editText.setShowSoftInputOnFocus(false);

editText.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        String text = editText.getText().toString();
        Intent myIntent = new Intent(view.getContext(), Calculated.class);
        myIntent.putExtra("mytext", text);
        startActivity(myIntent);
    }
});

这样,当您单击EditText时,将启动另一个活动。<br/>

如果您想要单击Button来启动活动,并且有一个带有ID button的按钮,则在Button上设置监听器:

button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        String text = editText.getText().toString();
        Intent myIntent = new Intent(view.getContext(), Calculated.class);
        myIntent.putExtra("mytext", text);
        startActivity(myIntent);
    }
});
英文:

Why did you wrap the listener of the EditText inside another listener?<br/>
Change to this:

editText.setInputType(InputType.TYPE_NULL);
editText.setShowSoftInputOnFocus(false);

editText.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        String text = editText.getText().toString();
        Intent myIntent = new Intent(view.getContext(),Calculated.class);
        myIntent.putExtra(&quot;mytext&quot;,text);
        startActivity(myIntent);
    }
});

this way when you click on the EditText you will start the other activity.<br/>

If you want to click a Button to start the activity, say with id button, then set the listener on the Button:

button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        String text = editText.getText().toString();
        Intent myIntent = new Intent(view.getContext(),Calculated.class);
        myIntent.putExtra(&quot;mytext&quot;,text);
        startActivity(myIntent);
    }
});

答案2

得分: 1

什么意思使用EditText输入来转到第二个活动?

如果您想要点击逻辑,请使用按钮。

如果您想要使用EditText的“完成”按钮:

editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
        if (i == EditorInfo.IME_ACTION_DONE) {
            String text = editText.getText().toString();
            Intent myIntent = new Intent(view.getContext(), Calculated.class);
            myIntent.putExtra("mytext", text);
            startActivity(myIntent);
            return true;
        }
        return false;
    }
});

在您的layout.xml中,在您的editText中添加:

android:inputType="text"
android:imeOptions="actionDone"
英文:

What do you mean by using the EditText input to go to a second activity ?

If you want a click logic, use a Button.

If you want to use the 'DONE' button of the EditText:

editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
            if (i == EditorInfo.IME_ACTION_DONE) {
                String text = editText.getText().toString();
                Intent myIntent = new Intent(view.getContext(),Calculated.class);
                myIntent.putExtra(&quot;mytext&quot;,text);
                startActivity(myIntent);
                return true;
            }
            return false;
        }
    });

On your layout.xml, on your editText you need to add:

    android:inputType=&quot;text&quot;
    android:imeOptions=&quot;actionDone&quot;

答案3

得分: 1

我认为,你应该使用Button,而不是EditText,但是如果你仍然想要的话;

Button btnClick;

btnClick = (Button) findViewById(R.id.btnClickActivity);

        btnClick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                startActivity(intent);
            }
        });

这样就可以了。

英文:

I think, you should use Button instead of Edittext but still if you want;

EditText edtClick;

edtClick = (EditText) findViewById(R.id.edtClickActivity);

        edtClick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                startActivity(intent);
            }
        });

That works.

huangapple
  • 本文由 发表于 2020年9月23日 23:49:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/64031727.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定