【问题标题】:how to display the name after the user inputted it用户输入后如何显示名称
【发布时间】:2018-05-25 22:54:00
【问题描述】:

如何在用户输入数据后显示名称以及如何使其成为一个按钮,我可以在那里添加一些东西使它类似于配置文件我希望你在其中添加一个警报但首先我必须制作这个数据库.

我真的不知道如何修复它我尝试了这段代码,但它不起作用,或者当我尝试添加时它总是导致强制关闭
name = edtName.getText().toString();

AccountInfo a = new AccountInfo(name);

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
    private TabLayout tabLayout;
    private AppBarLayout appBarLayout;
    private ViewPager viewPager;
    private EditText edtName;
    private Button btnAdd;
    Spinner spin1;
    Spinner spin2;
    Spinner spin3;


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


        tabLayout = findViewById(R.id.tablayout);
        appBarLayout = findViewById(R.id.bar);
        viewPager = findViewById(R.id.viewpager);

        edtName = findViewById(R.id.edtName);

        spin1 = findViewById(R.id.spinnerFrequency);
        spin2 = findViewById(R.id.SpinnerTime);
        spin3 = findViewById(R.id.SpinnerQty);
        btnAdd = findViewById(R.id.btnAdd);

        String  name = edtName.getText().toString();


        Adapter adapter = new Adapter(getSupportFragmentManager());
        /*screen*/
        adapter.AddFragment(new Drugfragment(), "Drug");
        adapter.AddFragment(new Appointmentfragment(), "Appointment");

        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setupWithViewPager(viewPager);

       /* DBHelper mDBHlpr = new DBHelper(this);
        AccountInfo a = new AccountInfo(name);
        mDBHlpr.insertRecord(a);


        String account_just_inserted = mDBHlpr.getAccountName();
        Log.d(
                "ACCOUNT INSERTED",
                "The Name of the account just inserted is :-" + account_just_inserted
        );*/



        final Account c = new Account();
        if(c==null) {

        }else{
           AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("LOG IN");
            LayoutInflater inflater = MainActivity.this.getLayoutInflater();
            builder.setView(inflater.inflate(R.layout.account,null));
                    builder.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {

                                    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                                    LayoutInflater inflater = MainActivity.this.getLayoutInflater();


                                    builder.setView(inflater.inflate(R.layout.accountname,null));
                                    builder.setPositiveButton("Yes",new DialogInterface.OnClickListener(){
                                        public void onClick(DialogInterface dialog, int id) {

                                        }
                                    });


                                    builder.show();
                                }
                            });



            builder.setNegativeButton("No",new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            System.exit(0);
                        }
                    });

            builder.show();


        }

    }


    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        String text = parent.getItemAtPosition(position).toString();
        Toast.makeText(parent.getContext(), text, Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }


    }

维护性

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
    private TabLayout tabLayout;
    private AppBarLayout appBarLayout;
    private ViewPager viewPager;
    private EditText edtName;
    private Button btnAdd;
    Spinner spin1;
    Spinner spin2;
    Spinner spin3;

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


        tabLayout = findViewById(R.id.tablayout);
        appBarLayout = findViewById(R.id.bar);
        viewPager = findViewById(R.id.viewpager);

        edtName = findViewById(R.id.edtName);

        spin1 = findViewById(R.id.spinnerFrequency);
        spin2 = findViewById(R.id.SpinnerTime);
        spin3 = findViewById(R.id.SpinnerQty);
        btnAdd = findViewById(R.id.btnAdd);



        Adapter adapter = new Adapter(getSupportFragmentManager());
        /*screen*/
        adapter.AddFragment(new Drugfragment(), "Drug");
        adapter.AddFragment(new Appointmentfragment(), "Appointment");

        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setupWithViewPager(viewPager);

       /* DBHelper mDBHlpr = new DBHelper(this);
        AccountInfo a = new AccountInfo(name);
        mDBHlpr.insertRecord(a);


        String account_just_inserted = mDBHlpr.getAccountName();
        Log.d(
                "ACCOUNT INSERTED",
                "The Name of the account just inserted is :-" + account_just_inserted
        );*/



        final Account c = new Account();
        if(c==null) {

        }else{
           AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("LOG IN");
            LayoutInflater inflater = MainActivity.this.getLayoutInflater();
            builder.setView(inflater.inflate(R.layout.account,null));
                    builder.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {

                                    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                                    LayoutInflater inflater = MainActivity.this.getLayoutInflater();


                                    builder.setView(inflater.inflate(R.layout.accountname,null));
                                    builder.setPositiveButton("Yes",new DialogInterface.OnClickListener(){
                                        public void onClick(DialogInterface dialog, int id) {

                                        }
                                    });


                                    builder.show();
                                }
                            });



            builder.setNegativeButton("No",new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            System.exit(0);
                        }
                    });

            builder.show();


        }

    }


    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        String text = parent.getItemAtPosition(position).toString();
        Toast.makeText(parent.getContext(), text, Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }


    }

【问题讨论】:

  • 抱歉先生已经编辑过了
  • name = edtName.getText().toString(); - 我在发布的代码中没有看到这一行。
  • 先生已经添加了
  • ID 为edtName&lt;EditText&gt; 似乎不在activity_main 布局中。
  • 我把它放在drugfragment中

标签: android database fragment


【解决方案1】:

您可以为此使用 textwatcher,每次 EditText 字段中的文本发生变化时,它都会更改您的名称字符串。

et1.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                // TODO Auto-generated method stub
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                // TODO Auto-generated method stub
            }

            @Override
            public void afterTextChanged(Editable s) {
              name = edtName.getText().toString();

    //it will change your name String every time when text is changing in EditText fiels
            }
        });

希望这是你想要的。

【讨论】:

    【解决方案2】:

    它相当简单。在 vb.net 上就是这样...... c# 只是略有不同。

        Dim b As New Button
        b.text = "New Button"
        b.Location = New Point(0, 0)
        AddHandler b.Click, AddressOf handle_click
    
        Private Sub handle_click(sender As Object, e As EventArgs)
         'code here to react to the buttons click.
        End Sub
    

    这是一个包含大量信息的链接。希望对您有所帮助 Controls in Java

    【讨论】:

    • java中有什么暗淡的?
    • 我不使用 java 但我认为你可以忽略它.. b 按钮;应该工作
    • 问题是关于 Java,而不是 VB.Net 或 C#。用 Java 发布代码,或者您没有回答所提出的问题。如果您根本不使用 Java,并且不懂该语言,那么您不应该发布代码来回答 Java 问题。
    • 是的..我仔细看了他的代码后明白了..对不起。
    猜你喜欢
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 2020-04-09
    • 2020-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-16
    相关资源
    最近更新 更多