onClick方法无法正常工作

时间:2016-01-21 10:47:23

标签: android

我正在尝试在 Eclipse 上开发 Android 5.0.1 的应用程序。我面对一些奇怪的事情,我已经抬头看了他们,但我找不到任何解决方案。 基本上,在此活动中:

package esp1415.xyz;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import android.app.Activity;
import android.content.Intent;
import android.media.RingtoneManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.TimePicker;

public class Sveglia extends Activity {

private DBHelper dbh = new DBHelper(this);
private DettagliFarmaco detfar;

private EditText ednome;
private EditText eddesc;
private EditText edqnt;
private EditText edind;
private TimePicker tp;
private CheckBox chksett;
private CheckBox chklun;
private CheckBox chkmar;
private CheckBox chkmer;
private CheckBox chkgio;
private CheckBox chkven;
private CheckBox chksab;
private CheckBox chkdom;
private TextView titsuon;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.alarm);
    getActionBar().setTitle("Inserisci i valori");

     ednome = (EditText) findViewById(R.id.ednome);
     eddesc = (EditText) findViewById(R.id.eddesc);
     edqnt = (EditText) findViewById(R.id.edqnt);
     edind = (EditText) findViewById(R.id.edind);
     tp = (TimePicker) findViewById(R.id.tp);
     chksett = (CheckBox) findViewById(R.id.chksett);
     chklun = (CheckBox) findViewById(R.id.chklun);
     chkmar = (CheckBox) findViewById(R.id.chkmar);
     chkmer = (CheckBox) findViewById(R.id.chkmer);
     chkgio = (CheckBox) findViewById(R.id.chkgio);
     chkven = (CheckBox) findViewById(R.id.chkven);
     chksab = (CheckBox) findViewById(R.id.chksab);
     chkdom = (CheckBox) findViewById(R.id.chkdom);
     titsuon = (TextView) findViewById(R.id.titolo_suoneria);

    long id = getIntent().getExtras().getLong("id");

    if (id == -1) {
        detfar = new DettagliFarmaco();
    } else {
        detfar = dbh.getFarmaco(id);

        ednome.setText(detfar.nome);
        eddesc.setText(detfar.descrizione);
        edqnt.setText(detfar.quantità);
        edind.setText(detfar.indice_associato);
        tp.setCurrentMinute(detfar.minuti);
        tp.setCurrentHour(detfar.ore);
        chksett.setChecked(detfar.ripsettimana);
        chklun.setChecked(detfar.getRipGiorno(DettagliFarmaco.LUNEDÌ));
        chkmar.setChecked(detfar.getRipGiorno(DettagliFarmaco.MARTEDÌ));
        chkmer.setChecked(detfar.getRipGiorno(DettagliFarmaco.MERCOLEDÌ));
        chkgio.setChecked(detfar.getRipGiorno(DettagliFarmaco.GIOVEDÌ));
        chkven.setChecked(detfar.getRipGiorno(DettagliFarmaco.VENERDÌ));
        chksab.setChecked(detfar.getRipGiorno(DettagliFarmaco.SABATO));
        chkdom.setChecked(detfar.getRipGiorno(DettagliFarmaco.DOMENICA));
        titsuon.setText(RingtoneManager.getRingtone(this, detfar.suoneria).getTitle(this));

        TextView contenitoreSuoneria = (TextView) findViewById(R.id.suoneria);
        contenitoreSuoneria.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
            startActivityForResult(intent , 1);
        }
        });

    Button sal = (Button) findViewById(R.id.salva);
    sal.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            salvataggio();

            TextView data = (Button) findViewById(R.id.data);
            String d = new SimpleDateFormat("dd-MM-YYYY", Locale.ITALY).format(new Date());
            data.setText(d);

            TextView ora = (Button) findViewById(R.id.ora);
            String o = new SimpleDateFormat("HH:mm", Locale.ITALY).format(new Date());
            ora.setText(o);

            DBHelper dbh = new DBHelper(Sveglia.this);
            if (detfar.id < 0) {
                dbh.createFarmaco(detfar);
            } else {
                dbh.updateFarmaco(detfar);
            }
            Intent in1 = new Intent(view.getContext(), ListaFarmaciActivity.class);
            startActivityForResult(in1, 0);

        }

});}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == RESULT_OK) {
        switch (requestCode) {
            case 1: {
                detfar.suoneria = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);   
                titsuon.setText(RingtoneManager.getRingtone(this, detfar.suoneria).getTitle(this));
                break;
            }
            default: {
                break;
            }
        }
    }
}

private void salvataggio() {

    detfar.nome = ednome.getText().toString();  
    detfar.descrizione = eddesc.getText().toString();
    detfar.quantità = edqnt.getText().toString();
    detfar.indice_associato = edind.getText().toString();
    detfar.minuti = tp.getCurrentMinute().intValue();
    detfar.ore = tp.getCurrentHour().intValue();
    detfar.ripsettimana = chksett.isChecked();
    detfar.setRipGiorno(detfar.LUNEDÌ, chklun.isChecked());
    detfar.setRipGiorno(detfar.MARTEDÌ, chkmar.isChecked());
    detfar.setRipGiorno(detfar.MERCOLEDÌ, chkmer.isChecked());
    detfar.setRipGiorno(detfar.GIOVEDÌ, chkgio.isChecked());
    detfar.setRipGiorno(detfar.VENERDÌ, chkven.isChecked());
    detfar.setRipGiorno(detfar.SABATO, chksab.isChecked());
    detfar.setRipGiorno(detfar.DOMENICA, chkdom.isChecked());
    detfar.attiva = true;
}
}

Textview contenitoreSuoneria的onClick方法不起作用。该视图是可点击的,但没有任何反应。此外,我为XML活动设置的背景没有出现在我用于测试的手机上(三星Galaxy Note 4)。

XML代码是:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
    android:isScrollContainer="true"
    android:background="@drawable/bg8" >

    <TableLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:shrinkColumns="*"
    android:stretchColumns="*">

        <TextView
            android:id="@+id/nome"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/nome"
            android:textColor="#FAEBD7" />

        <EditText
            android:id="@+id/ednome"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:backgroundTint="#00FFFF"
            android:ems="10"
            android:hint="@string/h1"
            android:inputType="text"
            android:textColor="#FAEBD7" />

        <TextView
        android:id="@+id/desc"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/desc"
        android:textColor="#FAEBD7" />

        <EditText
        android:id="@+id/eddesc"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:inputType="text"
        android:ems="10" 
        android:textColor="#FAEBD7"
        android:backgroundTint="#00FFFF"
        android:hint ="@string/h2"/>


        <TextView
        android:id="@+id/qnt"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/qnt"
        android:textColor="#FAEBD7" />

        <EditText
        android:id="@+id/edqnt"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:inputType="text"
        android:textColor="#FAEBD7"
        android:backgroundTint="#00FFFF"
        android:ems="10"
        android:hint ="@string/h3" />

        <TextView
         android:id="@+id/ind"
         android:layout_width="0dp"
         android:layout_height="wrap_content"
         android:layout_weight="1"
         android:text="@string/ind"
         android:textColor="#FAEBD7"  />

        <EditText
        android:id="@+id/edind"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:inputType="text"
        android:ems="10"
        android:textColor="#FAEBD7"
        android:backgroundTint="#00FFFF" 
        android:hint ="@string/h4"/>


    <TimePicker
        android:id="@+id/tp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:timePickerMode="clock" />  

    <TableRow
       android:layout_width="match_parent"
       android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/sett"
            android:textColor="#FAEBD7"/>

        <CheckBox
            android:id="@+id/chksett"
            style="android:checkboxStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="34dp" 
            android:buttonTint="#FAEBD7"/>

        </TableRow>
        <View
            android:id="@+id/div1"
            style="@style/linea" />


     <TableRow
       android:layout_width="match_parent"
       android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/lun"
            android:textColor="#FAEBD7"/>

        <CheckBox
            android:id="@+id/chklun"
            android:layout_marginLeft="34dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:buttonTint="#FAEBD7" />
    </TableRow>

    <TableRow
       android:layout_width="match_parent"
       android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/mar"
            android:textColor="#FAEBD7"/>

        <CheckBox
            android:id="@+id/chkmar"
            android:layout_marginLeft="34dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:buttonTint="#FAEBD7"/>
        </TableRow>

    <TableRow
       android:layout_width="match_parent"
       android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/mer"
            android:textColor="#FAEBD7"/>

        <CheckBox
            android:id="@+id/chkmer"
            android:layout_marginLeft="34dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:buttonTint="#FAEBD7" />
        </TableRow>

    <TableRow
       android:layout_width="match_parent"
       android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/gio"
            android:textColor="#FAEBD7"/>

        <CheckBox
            android:id="@+id/chkgio"
            android:layout_marginLeft="34dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:buttonTint="#FAEBD7" />
        </TableRow>

    <TableRow
       android:layout_width="match_parent"
       android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/ven"
            android:textColor="#FAEBD7"/>

        <CheckBox
            android:id="@+id/chkven"
            android:layout_marginLeft="34dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:buttonTint="#FAEBD7" />
        </TableRow>

    <TableRow
       android:layout_width="match_parent"
       android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/sab"
            android:textColor="#FAEBD7"/>

        <CheckBox
            android:id="@+id/chksab"
            android:layout_marginLeft="34dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:buttonTint="#FAEBD7"/>
        </TableRow>

    <TableRow
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/dom"
            android:textColor="#FAEBD7"/>

        <CheckBox
            android:id="@+id/chkdom"
            android:layout_marginLeft="34dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:buttonTint="#FAEBD7"/>
        </TableRow>

        <View
            android:id="@+id/div2"
            style="@style/linea" />

            <TextView
                android:id="@+id/suoneria"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/suo"
                android:textSize="18sp"
                android:textColor="#FAEBD7"
                android:background="@drawable/touch_selector" 
                android:clickable="true" />

            <TextView
                android:id="@+id/titolo_suoneria"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/sel"
                android:textSize="14sp"
                android:textColor="#FAEBD7" />

            <View
            android:id="@+id/div3"
            style="@style/linea" />

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/salva"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/imp"
            android:textColor="#FAEBD7"
            android:layout_marginLeft="100dp"/>
        </TableRow> 
</TableLayout>
</ScrollView>

有人能帮助我吗?非常感谢,我很抱歉我的英语。

3 个答案:

答案 0 :(得分:1)

简单明了

 <TextView
            android:id="@+id/suoneria"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/suo"
            android:textSize="18sp"
            android:textColor="#FAEBD7"
            android:background="@drawable/touch_selector" 
            android:clickable="true"
            android:onClick="click" />

在您的活动中

public void click(View view)
{
 // these lines executes when you click on your textview.
 }

所有其他事情看起来都不错。请试一试。

答案 1 :(得分:0)

尝试在文本视图中添加while volume.status != 'available': print("Volume status [%s]" % volume.status) time.sleep(1.0) volume = cinder.volumes.get(volume.id) 。看起来padding,触摸不适用于due to lesser content

<强>更新textview

上试试setFocusable(true)

答案 2 :(得分:0)

尝试在xml文件中使用android:onClick