Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何通过单击其他按钮添加新按钮_Android_Button_Android Linearlayout - Fatal编程技术网

Android 如何通过单击其他按钮添加新按钮

Android 如何通过单击其他按钮添加新按钮,android,button,android-linearlayout,Android,Button,Android Linearlayout,我需要通过单击另一个按钮(添加新按钮)来添加一个按钮。我的按钮代码添加新按钮 addnew = (Button)findViewById(R.id.btnaddnew); addnew.setOnClickListener(this); public void onClick(View v) { if(v == addnew) { Button myButton = new Button(this); myButton.setText("New B

我需要通过单击另一个按钮(添加新按钮)来添加一个按钮。我的按钮代码添加新按钮

addnew = (Button)findViewById(R.id.btnaddnew);
    addnew.setOnClickListener(this);
public void onClick(View v) {
if(v == addnew)
    {
        Button myButton = new Button(this);
        myButton.setText("New Button");
        myButton.setId(some_random_id);
        LinearLayout ll = (LinearLayout)findViewById(R.id.layout1);
        LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        ll.addView(myButton, lp);
    }
}
上面的代码可以很好地将按钮添加到布局中,但在关闭应用程序后,当我再次打开它时,先前添加的新按钮不存在。有人能帮忙吗

在实现SharedReferences之后 onCreate的代码

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ListView lvInb = (ListView) findViewById(R.id.lvInb);
    addnew = (Button)findViewById(R.id.btnaddnew);
    addnew.setOnClickListener(this);
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
onClick的代码

public void onClick(View v) {
if(v == addnew)
    {
        count = prefs.getInt("count", 0);
        for(int i=0;i<count;i++){
        Button myButton = new Button(this);
        myButton.setText("New Button");
        LinearLayout ll = (LinearLayout)findViewById(R.id.layout1);
        LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        ll.addView(myButton, lp);
        count++;
        Editor editor = prefs.edit();
        editor.putInt("count", count);
        editor.commit();
    } }
public void onClick(视图v){
如果(v==addnew)
{
count=prefs.getInt(“count”,0);

对于(int i=0;i您需要将创建的按钮永久保存在某个位置。您使用的代码不会更改将加载到
onCreate()中的布局xml
活动的
。您可以使用
SharedReferences
保存您创建的
按钮
的数量,然后在
onCreate()中的
setContentView()
之后按代码添加它们

您需要添加在
SharedReferences
中创建的按钮数量,并在创建时循环到该计数并创建这些按钮

在onCreate中,通过以下方式获取SharedRef:

prefs = PreferenceManager.getDefaultSharedPreferences(this);
进行全局变量计数:

int count=0;
当您添加新按钮时,增加计数:

addnew = (Button)findViewById(R.id.btnaddnew);
    addnew.setOnClickListener(this);
public void onClick(View v) {
if(v == addnew)
    {
        Button myButton = new Button(this);
        myButton.setText("New Button");
        myButton.setId(some_random_id);
        LinearLayout ll = (LinearLayout)findViewById(R.id.layout1);
        LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        ll.addView(myButton, lp);
        count++;
        Editor editor = prefs.edit();
    editor.putInt("count", count);
    editor.commit();
    }
}
在onCreate中:

count=prefs.getInt("count", 0);
for(int i=;i<count;i++){
        Button myButton = new Button(this);
        myButton.setText("New Button");
        myButton.setId(some_random_id);
        LinearLayout ll = (LinearLayout)findViewById(R.id.layout1);
        LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        ll.addView(myButton, lp);
}
prefs = PreferenceManager.getDefaultSharedPreferences(this); 
在onCreate中:

count=prefs.getInt("count", 0);
for(int i=;i<count;i++){
        Button myButton = new Button(this);
        myButton.setText("New Button");
        myButton.setId(some_random_id);
        LinearLayout ll = (LinearLayout)findViewById(R.id.layout1);
        LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        ll.addView(myButton, lp);
}
prefs = PreferenceManager.getDefaultSharedPreferences(this); 

您应该阅读基础知识,因为您的问题是关于应用程序和活动生命周期的状态。您不应该在没有这些知识的情况下开始编码。检查此链接是否可以告诉我需要在何处添加共享首选项您可以在创建新按钮以保存此按钮时添加它,或者您可以在onPause()中添加它要保存应用程序退出视图前的按钮总数,请告诉我需要在何处为“count=prefs.getBoolean(“count”,0);”显示错误类型SharedReferences中的getBoolean(String,boolean)方法不适用于参数(String,int)抱歉,这是一个输入错误呼叫getInt而不是当点击应用程序中的addnew按钮时它崩溃了你能发布你的日志吗?