您的位置:首页 > 移动开发 > Android开发

Android 动态生成多个控件并实现点击

2016-11-16 17:40 441 查看
我们来看下动态效果图,如下:



首先这个按钮是根据所填的数动态生成的,然后还要设置他的点击事件。这个demo需要两个layout和一个activity:
首先来看下这两个布局文件:
1.这个是界面的布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.cumnication_album.active_create_btn.MainActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:text="请输入title标签的个数" />

<EditText
android:id="@+id/num_et"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:inputType="number"
android:maxLength="1" />

<Button
android:id="@+id/num_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#455178"
android:textColor="#fff"
android:text="确定" />
</LinearLayout>

<LinearLayout
android:id="@+id/titles_ll"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#455178"
android:orientation="horizontal"></LinearLayout>
<TextView
android:id="@+id/shownum_tv"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
2.第二个layout就是加载的button的layout了

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal"
android:padding="5dp">
<Button
android:id="@+id/top_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/bg_titlegroup"
android:gravity="center"
android:textColor="@drawable/bg_item_btn"
android:textSize="15sp" />
</LinearLayout>


接下来就是activity的代码了

public class MainActivity extends AppCompatActivity {

private EditText num_et;
private Button num_btn;
private LinearLayout titles_ll;
private TextView shownum_tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init(){
num_et=(EditText)findViewById(R.id.num_et);
num_btn=(Button)findViewById(R.id.num_btn);
titles_ll=(LinearLayout)findViewById(R.id.titles_ll);
shownum_tv=(TextView)findViewById(R.id.shownum_tv);
num_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(TextUtils.isEmpty(num_et.getText().toString())){
Toast.makeText(MainActivity.this, "请先填个数", Toast.LENGTH_SHORT).show();
return;
}
if(num_et.getText().toString().equals("0")){
Toast.makeText(MainActivity.this, "不能填0", Toast.LENGTH_SHORT).show();
return;
}
int num=Integer.parseInt(num_et.getText().toString());
titles_ll.removeAllViews();
for (int i=0;i<num;i++){
final LinearLayout ll= (LinearLayout) LayoutInflater.from(MainActivity.this).inflate(R.layout.combin_item_button,null);
ll.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1));//此处设置权重
final Button btn=(Button)ll.findViewById(R.id.top_one);
int m=i+1;
btn.setText("第"+m+"个按钮");
titles_ll.addView(ll);
}

for(int j=0;j<titles_ll.getChildCount();j++){
final Button bt=(Button) titles_ll.getChildAt(j).findViewById(R.id.top_one);
final int finalJ = j;
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for(int m=0;m<titles_ll.getChildCount();m++){
titles_ll.getChildAt(m).findViewById(R.id.top_one).setEnabled(true);
}
bt.setEnabled(false);
shownum_tv.setText("您点击了"+bt.getText().toString());
}
});
}
titles_ll.getChildAt(0).findViewById(R.id.top_one).performClick();
titles_ll.getChildAt(0).findViewById(R.id.top_one).setEnabled(false);
}
});
}

}
以上就是这个demo的代码。

想要完整demo,可以点这里哦!http://download.csdn.net/detail/aa_chao/9684921
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: