如何在用户输入数据后显示名称,以及如何使其成为一个按钮,我可以添加一些东西,使其像一个配置文件,我希望你在其中添加一个警报,但首先我要制作这个数据库
我真的不知道如何修复它我试过这段代码但是它不起作用或者当我试图添加一个力时它总是导致一个力关闭 name = edtName.getText()。toString();
AccountInfo a =新的AccountInfo(名称);
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) {
}
}
MAINACTIVITY
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) {
}
}
答案 0 :(得分:0)
您可以使用textwatcher,每次在EditText字段中更改文本时,它都会更改您的名称String。
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
}
});
希望这就是你想要的。
答案 1 :(得分:-1)
它相当简单。在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