我正在创建一个Android应用,我希望在其中添加button
动态label
。
Activity
的流程如下: -
Activity
加载时,它会询问用户" HOW MANY BUTTONS USER WANTS?"
例如: - 如果用户输入4 新活动将以4 EditText(s)
打开,其中用户将提供Buttons
的名称
例如: - 播放,暂停,停止,倒带
当用户提供姓名时,最终Activity
会打开四个按钮,其名称为PLAY,PAUSE,STOP,REWIND。
我完成了第1部分和第2部分但面临第3部分的问题。有任何帮助吗?
第一项活动代码: -
package com.example.dynamicbutton;
public class MainActivity extends Activity
{
private Button button,button1;
EditText et;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.addnames);
et = (EditText) findViewById(R.id.editText);
button1.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
String theText = et.getText().toString();
Intent intent = new Intent(MainActivity.this,NameActivity.class);
intent.putExtra("text_names", theText);
startActivity(intent);
}
});
}
}
第二项活动的代码: -
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
Intent i = getIntent();
Bundle extras = i.getExtras();
String m = extras.getString("text_label");
int m1=Integer.parseInt(m);
super.onCreate(savedInstanceState);
setContentView(R.layout.dynamically_create_view_element);
final LinearLayout lm = (LinearLayout) findViewById(R.id.linearMain);
// create the layout params that will be used to define how your
// button will be displayed
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//Create four
for(int j=1;j<=m1;j++)
{
// Create LinearLayout
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.HORIZONTAL);
// Create Button
final Button btn = new Button(this);
// Give button an ID
btn.setId(j+1);
btn.setText("Add To Cart"+j );
// set the layoutParams on the button
btn.setLayoutParams(params);
final int index = j;
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.i("TAG", "index :" + index);
Toast.makeText(getApplicationContext(), "Clicked Button Index :" + index, Toast.LENGTH_LONG).show();
}
});
//Add button to LinearLayout
ll.addView(btn);
//Add button to LinearLayout defined in XML
lm.addView(ll);
}
}
}
答案 0 :(得分:0)
这很容易。 只需按照以下步骤操作: -
Layout
。EditText
作为子对象的布局。LinearLayout
。LayoutInflater
类在For-Loop中对EditText进行充气,以动态加载该布局用户输入的次数。LayoutInflater
动态加载按钮布局,并从ArrayList
答案 1 :(得分:-1)
以下是您最终活动的代码
import android.os.Bundle;
import android.app.Activity;
import android.app.ActionBar.LayoutParams;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class FinalActivity extends Activity {
String caption[4]={"play","edit","undo","delete"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.YourLayout);
final LinearLayout lm = (LinearLayout) findViewById(R.id.linearMain); // linearMain //is your linearlayout in XML file
// create the layout params that will be used to define how your
// button will be displayed
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//Create four
for(int j=0;j<=4;j++)
{
// Create LinearLayout
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.HORIZONTAL);
// Create TextView
Button btn= new Button(this);
btn.setText(caption[j]+j+" ");
ll.addView(btn);
// Create TextView
TextView lbl= new TextView(this);
price.setText(j+" ");
ll.addView(lbl);
// Create Button
final Button btn = new Button(this);
// Give button an ID
btn.setId(j+1);
btn.setText("Add To Cart");
// set the layoutParams on the button
btn.setLayoutParams(params);
final int index = j;
// Set click listener for button
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.i("TAG", "index :" + index);
Toast.makeText(getApplicationContext(),
"Clicked Button Index :" + index,
Toast.LENGTH_LONG).show();
}
});
//Add button to LinearLayout
ll.addView(btn);
//Add button to LinearLayout defined in XML
lm.addView(ll);
}
}
}