我有一个带有两个选项卡的活动。在第一个中有一个Button
. 如果按下此按钮,则会在第二个选项卡中创建Button
一个新选项卡。TableRow
这工作正常。
每个TableRow
都有 3 个视图(一个图像、一些文本和一个button
)。每次Button
创建 a 时,我都会为其分配一个 id (0,1,2 ...)。
如果 id 是 0,1,2 ......我想要的是设置一个特定的文本......无论哪个 id。
我已经尝试过了,但它不起作用:
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch(arg0.getId()){
case R.id.save:
if (et.getText() !=null && thumbnail != null){
TableRow tr = new TableRow(this);
ImageView view = new ImageView(this);
TextView view2 = new TextView(this);
Button view3 = new Button(this);
view3.setOnClickListener(this);
titulo = new String[500];
mensaje = new String[500];
fotos = new Bitmap[500];
view.setImageBitmap(thumbnail);
view.setPadding(1, 5, 0, 0);
view.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
Calendar c = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
titulo[i]=""+et.getText();
mensaje[i]=""+et1.getText();
fotos[i]=thumbnail;
i++;
view2.setText("" + et.getText() + dateFormat.format(c.getTime()) );
view2.setGravity(Gravity.CENTER_HORIZONTAL);
view2.setGravity(Gravity.CENTER_VERTICAL);
view3.setGravity(Gravity.RIGHT);
view3.setGravity(Gravity.CENTER_VERTICAL);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
tr.addView(view, metrics.widthPixels/3, 150);
tr.addView(view2, metrics.widthPixels/2, 100);
tr.addView(view3, metrics.widthPixels/6, 20);
tl.addView (tr, 0);
final Toast toastMensaje = Toast.makeText(getApplicationContext(),
"Tu entrada se cargó correctamente", Toast.LENGTH_LONG);
toastMensaje.setGravity(Gravity.CENTER, 0, 0);
toastMensaje.show();
et.setText("");
et1.setText("");
i1.setVisibility(View.GONE);
}
else{
final Toast toastMensaje = Toast.makeText(getApplicationContext(),
"Tienes que añadir un título y una foto", Toast.LENGTH_LONG);
toastMensaje.setGravity(Gravity.CENTER, 0, 0);
toastMensaje.show();
}
break;
case R.id.photo:
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
break;
case R.id.gallery:
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, TFRequestCodes);
break;
}
for (int j=0; j<titulo.length; j++){
if (arg0.getId()==j){
final Toast toastMensaje = Toast.makeText(getApplicationContext(),titulo[j], Toast.LENGTH_LONG);
toastMensaje.setGravity(Gravity.CENTER, 0, 0);
toastMensaje.show();
}
}
}
这段代码不起作用:
for (int j=0; j<titulo.length; j++){
if (arg0.getId()==j){
final Toast toastMensaje = Toast.makeText(getApplicationContext(),titulo[j], Toast.LENGTH_LONG);
toastMensaje.setGravity(Gravity.CENTER, 0, 0);
toastMensaje.show();
}
}
这是 XML 文件:
android:id="@+id/LinearLayout"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabHost
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#001521"
android:orientation="vertical">
<EditText
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="42dp"
android:layout_marginTop="5dp"
android:hint="Lugar"
android:gravity="top">
</EditText>
<EditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="252dp"
android:layout_marginTop="3dp"
android:hint="Título"
android:gravity="top"
android:inputType="textMultiLine" ></EditText>
<LinearLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#001521"
android:orientation="horizontal">
<Button
android:id="@+id/photo"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="Photo" />
<Button
android:id="@+id/gallery"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="Gallery" />
<ImageView
android:id="@+id/i1"
android:layout_width="wrap_content"
android:layout_height="50dp"/>
</LinearLayout>
<Button
android:id="@+id/save"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Guardar entrada"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<TableLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/tl"
android:stretchColumns="0,1,2">
</TableLayout>
</ScrollView>
</LinearLayout>
<LinearLayout
android:id="@+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>