我已經構建了一個android應用程序,當用戶按下按鈕時,該應用程序會在UI上創建一個textview和一個edittext。目前,當你按'添加'按鈕時,創建了兩個字段,但編輯文本出現在textview下面,我希望edittext出現在屏幕上,並與textview的右側對齊,如下所示:編程創建並將texttext對齊到文本視圖的右邊
[addbutton]
[TextView的] [的EditText]
[TextView的] [的EditText]
[TextView的] [的EditText]
代碼:
private OnClickListener addForm() {
return new OnClickListener() {
@Override
public void onClick(View v) {
mLayout.addView(createNewTextView(numberString));
mLayout.addView(createNewEditText(null));
numberInt = Integer.parseInt(numberString);
numberInt++;
numberString = Integer.toString(numberInt);
}
};
}
// add textview
private TextView createNewTextView(String text) {
final TextView textView = new TextView(this);
textView.setLayoutParams(lparams);
textView.setText(text);
return textView;
}
// add edittext
private EditText createNewEditText(String text) {
final EditText editText = new EditText(this);
editText.setLayoutParams(lparams);
editText.setText(text);
return editText;
}
修訂1:
行,所以我改了一下週圍的代碼我認爲應該工作,但EditText上仍出現以下任何人都可以看到我要去哪裏錯了或改變我的代碼,使其工作?感謝
新代碼:
private OnClickListener add() {
return new OnClickListener() {
@Override
public void onClick(View v) {
createNewTextView(numberString);
numberInt = Integer.parseInt(numberString);
numberInt++;
numberString = Integer.toString(numberInt);
}
};
}
// add textview
private void createNewTextView(String text) {
final TextView textView = new TextView(this);
textView.setLayoutParams(lparams);
textView.setText(text);
textView.setId(numberInt);
mLayout.addView(textView);
createNewEditText(textView);
}
// add edittext
private void createNewEditText(TextView textView) {
final EditText editText = new EditText(this);
lparams.addRule(RelativeLayout.RIGHT_OF, textView.getId());
editText.setLayoutParams(lparams);
mLayout.addView(editText);
}
修訂2:
所有Java和XML以下代碼:
的Java:
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private LinearLayout mLayout;
private Button mButton;
private String numberString = "1";
private int numberInt = 1;
final LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLayout = (LinearLayout) findViewById(R.id.linearLayout);
mButton = (Button) findViewById(R.id.button);
mButton.setOnClickListener(add());
TextView textView = new TextView(this);
textView.setText("New text");
}
private OnClickListener add() {
return new OnClickListener() {
@Override
public void onClick(View v) {
mLayout.addView(createNewTextView(numberString));
mLayout.addView(createNewEditText(null));
numberInt = Integer.parseInt(numberString);
numberInt++;
numberString = Integer.toString(numberInt);
}
};
}
// add textview
private TextView createNewTextView(String text) {
final TextView textView = new TextView(this);
textView.setLayoutParams(lparams);
textView.setText(text);
return textView;
}
// add edittext
private EditText createNewEditText(String text) {
final EditText editText = new EditText(this);
editText.setLayoutParams(lparams);
editText.setText(text);
editText.setId(numberInt);
int i = editText.getId();
Toast toast= Toast.makeText(MainActivity.this,"edittext ID:" + i , Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, -100);
toast.show();
return editText;
}
}
XML:
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add+"
/>
我如何設置id對他們來說? –
像這樣textView.setId(1); – Vamshi
你解決了這個問題嗎? – Vamshi