我会在业余时间开发Android应用程序,以了解Android开发。我正在尝试制作成绩/ GPA计算器应用程序。我目前有一个名为“ + New Semester”的按钮,其目的是打开一个弹出窗口,用户可在其中输入学期名称。在以下两个图像中可以看出:
用户单击“ +新学期”,出现弹出窗口,提示用户添加该学期的名称。
现在,我想做的是,当用户单击“完成”按钮时,将在“ +新学期”按钮下方创建一个新按钮,其中包含用户在文本框中键入的文本。弄清楚怎么做。我将不胜感激任何帮助。
这是我目前拥有的代码:
package com.example.gradecalculator;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
// Private Fields
private Dialog d;
private ImageButton newSemesterButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
d = new Dialog(this);
}
// When user clicks on "+ New Semester" button open a popup where the user is prompted to
// type in the Semester Name and when "Done" is clicked the new semester appears in the view
public void newSemesterPopup(View v) {
TextView closePopup;
ImageButton doneButton;
d.setContentView(R.layout.new_semester_popup);
doneButton = (ImageButton) d.findViewById(R.id.doneButton);
doneButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openMainActivity();
}
});
closePopup = (TextView) d.findViewById(R.id.exitButton);
closePopup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
d.dismiss();
}
});
d.show();
}
// Open Main Activity
public void openMainActivity() {
Intent main = new Intent(this, MainActivity.class);
startActivity(main);
}
}
您可以通过首先获取要在其中添加按钮的布局的引用来实现。假设按钮应该位于的布局名称为btn_layout,
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); Button btn = new Button(this); btn.setText("Your text from edittext"); layout.addView(btn, params);
如果您可以将Button和Layout声明为字段,那就更好了
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句