TextEdit内容不对

时间:2017-01-07 11:36:32

标签: java android

所以我有两个EditText字段。一个用于类别,另一个用于在其中放置链接。如果单击“weiter”按钮,则EditText为空时有一个控件。在下一步中,我想确保EditText链接中的链接是真实链接,而不仅仅是只有一个字。所以我想检查它是否包含www.,现在问题是如果它不包含它我怎么能休息?

新的Intent启动是因为我将其设置为明确的下一个if-else

那我怎样才能让它变得更好?

这是我的代码,谢谢大家:

((Button) findViewById(R.id.weiter)).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        Intent i = new Intent(MainActivity2.this, MainActivity8.class);

        EditText et1 = (EditText) findViewById(R.id.editText2);
        String Link1 = et1.getText().toString();

        EditText et = (EditText) findViewById(editText1);
        String Kategorie1 = et.getText().toString();

        if (Link1.contains("www.")) {

        } else {
            et1.setError("Link ist nicht gültig");
        }

        if (Link1.matches("") || Kategorie1.matches("")) {
            et.setHintTextColor(RED);
            et1.setHintTextColor(RED);
        } else {
            i.putExtra("ersteActivityKategorie", Kategorie1);
            i.putExtra("ersteActivityLink", Link1);
            startActivity(i);
        }
    }
});

3 个答案:

答案 0 :(得分:0)

return当您在www开头找不到String时,只需if (Link1.contains("www.")) { // Do nothing } else { et1.setError("Link ist nicht gültig"); et1.setHintTextColor(RED); return; // Return from here } if (Link1.matches("") || Kategorie1.matches("")) { et.setHintTextColor(RED); et1.setHintTextColor(RED); } else { i.putExtra("ersteActivityKategorie", Kategorie1); i.putExtra("ersteActivityLink", Link1); startActivity(i); }

private NestedClass nestedClass = new NestedClass();

答案 1 :(得分:0)

public class Trackfolio extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    public EditText editText;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
        editText = (EditText)findViewById(R.id.editText1);

        editText.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        editText.setText("");
    }
}

答案 2 :(得分:0)

Intent i = new Intent(MainActivity2.this, MainActivity8.class);

        EditText et1 = (EditText) findViewById(R.id.editText2);
        String Link1 = et1.getText().toString();

        EditText et = (EditText) findViewById(editText1);
        String Kategorie1 = et.getText().toString();

        if (isValidUrl(Link1) == true) {

        } else {
            et1.setError("Link ist nicht gültig");
        }

        if ((Link1.equals("") && Kategorie1.equals("")) || (Link1.length() == 0 && Kategorie1.length() == 0) || (Link1.matches("") || Kategorie1.matches(""))) {
            et.setHintTextColor(RED);
            et1.setHintTextColor(RED);
        } else {
            i.putExtra("ersteActivityKategorie", Kategorie1);
            i.putExtra("ersteActivityLink", Link1);
            startActivity(i);
        }

        private boolean isValidUrl(String url) {
            Pattern p = Patterns.WEB_URL;
            Matcher m = p.matcher(url.toLowerCase());
            if(m.matches())
                return true;
            else
                return false;
        }
    }