我正在尝试将edittext添加到对话框中,低于其他对象或高于其他对象但是当我尝试在对话框中添加edittext时,它会出现在每个新编辑文本的上方。我得到了什么....
但我这样想......
当我点击图像添加按钮时,应在屏幕中添加新的textview。我编写了如下代码......
final RelativeLayout.LayoutParams params= new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
try {
Log.w("Addfriendsactivity", "friendsCount--first time-> "+friendsCount);
friendsCount++;
EditText textview = new EditText(context);
Log.w("Addfriendsactivity", "friendsCount---> "+friendsCount);
textview.setId(friendsCount);
params.addRule(RelativeLayout.BELOW, friendsCount--);
friendsCount += 1;
Log.w("Addfriendsactivity", "friendsCount--third time-> "+friendsCount);
textview.setLayoutParams(params);
textview.setVisibility(View.VISIBLE);
Log.w("Addfriendsactivity", "params---> "+params);
Log.w("Addfriendsactivity", "textview---> "+textview);
Log.w("Addfriendsactivity", "relativelayout--> "+textview);
dialog.addContentView(textview ,params);
textview.setText(String.valueOf(friendsCount));
Log.d("addFriendsActivity", "ashish comes here ");
}
catch (Exception e) {
e.printStackTrace();
}
});
}
任何人都能帮助我解决这个问题的原因吗? 提前谢谢..
修改
我的XML布局如下
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
tools:context=".AddFriendsCustomActivity" >
<EditText
android:id="@+id/writename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="24dp"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/submit_dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/writename"
android:layout_below="@+id/add"
android:layout_marginRight="46dp"
android:text="Submit" />
<ImageView
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/writename"
android:layout_marginLeft="22dp"
android:layout_toRightOf="@+id/writename"
android:contentDescription="Add Friend"
android:src="@drawable/add" />
</RelativeLayout>
答案 0 :(得分:0)
放置此行 - final RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);内部按钮的点击事件。见:
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
final RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Log.w("Addfriendsactivity", "friendsCount--first time-> "
+ friendsCount);
friendsCount++;
EditText textview = new EditText(MainActivity.this);
Log.w("Addfriendsactivity", "friendsCount---> " + friendsCount);
if (friendsCount == 2) {
textview.setId(friendsCount);
} else {
textview.setId(friendsCount);
params.addRule(RelativeLayout.BELOW, textview.getId() - 1);
}
// friendsCount += 1;
Log.w("Addfriendsactivity", "friendsCount--third time-> "
+ (textview.getId() - 1));
textview.setLayoutParams(params);
textview.setVisibility(View.VISIBLE);
Log.w("Addfriendsactivity", "params---> " + params);
Log.w("Addfriendsactivity", "textview---> " + textview);
Log.w("Addfriendsactivity", "relativelayout--> " + textview);
dialog.addView(textview);
textview.setText(String.valueOf(friendsCount));
Log.d("addFriendsActivity", "ashish comes here ");
}
});
答案 1 :(得分:0)
我只是在用户点击EditText
时创建创建Button
的示例。检查出来并根据您的要求进行更新。
<强> activity_main.xml中强>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="ShowDialog"
android:text="Click Me" />
</RelativeLayout>
layout_row.xml(对话框布局)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/layoutmain"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
<Button
android:id="@+id/btnadd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="add EditText" />
</LinearLayout>
<强> MainActivity.java 强>
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void ShowDialog(View v) {
Toast.makeText(getApplicationContext(), "Dialog!", Toast.LENGTH_LONG)
.show();
final Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.layout_row);
dialog.setTitle("Title...");
final LinearLayout layoutmain = (LinearLayout) dialog
.findViewById(R.id.layoutmain);
Button btnadd = (Button) dialog.findViewById(R.id.btnadd);
btnadd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
EditText myedittext = new EditText(MainActivity.this);
layoutmain.addView(myedittext);
}
});
dialog.show();
}
}
输出