Android Studio App EditText获取输入的文字

aedittext.getText().toString()

获取输入的字符串。

示范

整个activity_mani.xmln内容如下:


    
    

整个ManiActivity.java内容如下:

package netpythontojavaviewmodecontents.csdn.httpblog.timerun;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener{
    private EditText input;
    private Button enter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        enter.setOnClickListener(this);
    }
    private void initView()
    {
        input=(EditText) findViewById(R.id.input);
        enter=(Button) findViewById(R.id.enter);
    }
    public void onClick(View v){
        switch (v.getId()){
            case R.id.enter:
                Toast.makeText(this, input.getText().toString(), Toast.LENGTH_LONG).show();
                break;
            default:
                break;

        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings)
        {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

最终效果:

Android Studio App EditText获取输入的文字_第1张图片

你可能感兴趣的:(Android,App开发)